9 Replies - 1290 Views - Last Post: 27 July 2013 - 03:07 PM Rate Topic: -----

#1 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

How to decrement by 5 and user input in loop

Posted 27 July 2013 - 01:29 PM

I was just wondering how to decrement by 5(pre-dec and post-dec). Also, is my user input for the loop correct?

for (int age = input.nextInt(); age <= 65 && age > 0; age-5){
			System.out.println("What do miss about being " + age + " years old: ?");
			String life = input.nextLine();
		}


This post has been edited by streek405: 27 July 2013 - 02:21 PM

Is This A Good Question/Topic? 0
  • +

Replies To: How to decrement by 5 and user input in loop

#2 ConciselyVerbose   User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 315
  • Joined: 05-July 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 01:48 PM

age-=5
Was This Post Helpful? 1
  • +
  • -

#3 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 01:48 PM

Do decrement in the loop
Was This Post Helpful? 0
  • +
  • -

#4 StrongJoshua   User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 01:50 PM

age-=5

This will subtract 5 from age and assign that value back to age. Also, if you have the first part of the if inside the for loop(age <= 65) you are preventing anyone above the age of 65 to use this program. This may be your design choice, but I just wanted to let you know. Besides that, your code looks good from what you've posted.

Hope this helps :)
Was This Post Helpful? 1
  • +
  • -

#5 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 02:02 PM

View PostConciselyVerbose, on 27 July 2013 - 01:48 PM, said:

age-=5

Thanks how would I do it for a pre-decrement of 5?
Was This Post Helpful? 0
  • +
  • -

#6 StrongJoshua   User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 02:06 PM

View Poststreek405, on 27 July 2013 - 02:02 PM, said:

Thanks how would I do it for a pre-decrement of 5?

I don't think I understand the question. When do you want to decrement? Before the for loop or do you mean something else?
Was This Post Helpful? 0
  • +
  • -

#7 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 02:11 PM

View PostStrongJoshua, on 27 July 2013 - 02:06 PM, said:

View Poststreek405, on 27 July 2013 - 02:02 PM, said:

Thanks how would I do it for a pre-decrement of 5?

I don't think I understand the question. When do you want to decrement? Before the for loop or do you mean something else?

Here is the current code now:
while (age <= 65 && age > 0){
			System.out.println("What do miss about being " + age + " years old: ?");
			String life = input.nextLine();
			age-=5;
		}



The problem I am having now is that when I run it it prints out, twice before the user can input their answer. When I enter my age (22) it does this:
What do miss about being 22 years old: ?
What do miss about being 17 years old: ?

After the 2nd print its fine. So Im wondering if I should do a predecrement like --age instead of age--, or would that not change the outcome?
Was This Post Helpful? 0
  • +
  • -

#8 streek405   User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 02:24 PM

View PostStrongJoshua, on 27 July 2013 - 01:50 PM, said:

age-=5

This will subtract 5 from age and assign that value back to age. Also, if you have the first part of the if inside the for loop(age <= 65) you are preventing anyone above the age of 65 to use this program. This may be your design choice, but I just wanted to let you know. Besides that, your code looks good from what you've posted.

Hope this helps :)/>

Thank you. And I already know that, 65 was just some arbitrary number that I chose lol. How would I do a pre-drecement for 5? I tried 5=-age but that didnt work
Was This Post Helpful? 0
  • +
  • -

#9 Witchking   User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 189
  • Joined: 17-February 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 03:06 PM

You could use
System.out.println("xxx " + (age -= 5) + " yyy");

Was This Post Helpful? 0
  • +
  • -

#10 Golossos   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 118
  • Joined: 29-June 13

Re: How to decrement by 5 and user input in loop

Posted 27 July 2013 - 03:07 PM

I would also like to add (just for the sake of the integrity of input validation in question) that the condition should be between the ages of [5, 65], as follows. This is simply because if the user entered the age "3", the prompt would return a negative age, which is impossible.

The "-=" operation is correct; it is just a shorthand version of updating a value. Think of it being equivalent to:

age = age - 5;


And for your pre-decrement problem, just move the age decrement statement before the System.out prompt. This will update the age and print it correctly.

while (age <= 65 && age >= 5){
			age-=5;
			System.out.println("What do miss about being " + age + " years old: ?");
			String life = input.nextLine();
		}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1