Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with loop logic

 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basic java code kind of forgotten need some hel



this will show the following:

NO ODD NUMBERS
5 1 3 5
10
15



but what i want is :
NO ODD NUMBERS
5 1 3 5
10 1 3 5 7 9
15 1 3 5 7 9 11 13 15


thanks guys hope to hear from you
o




 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:thanks guys hope to hear from you

Question: what doesdo?

Answer that, and I think you'll work out what your problem is.

Winston
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while (z<5) {
z=z+2;
...
}

do?

Answer that, and I think you'll work out what your problem is.


i know it will keep displaying numbers up to 5 and z increments by 2.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:i know it will keep displaying numbers up to 5 and z increments by 2.


Right. But you don't want 5 to be your upper boundary, do you?
And what value does z have in your second and third loop?
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only for the first line

2nd line will be 9
3rd will be 15
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please write meaningful thread titles. I changed it to one I think describes the topic better.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction

kind of confused now
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion is always to put in tons of System.out.println() statements to see what your code is doing. Specifically, I would put some in here to see what x and z are at the beginning of each loop.

print out the values each time you think they should change. Print out the values just before any test-condition..i.e. just before each 'while' statement.

I don't think 'z' is what you think it is everywhere...
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
z is the odd numbers
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step through the loops with paper and pencil, note down the values each time through the loop. You'll see what Fred is trying to tell you.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:z is the odd numbers


No. z is exactly one odd number at any given moment, this value is its state. You should check this state at the start and / or end of every loop, simply with System.out.println(z);. You should do the same for x as well.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:z is the odd numbers

No it isn't (at least not based on what you wrote).

My advice: Stop guessing, and listen to the advice you've been given (especially fred's). Do some debugging and find out what is going on, because you aren't going to learn anything by simply firing off questions.

Winston
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe this will make it abit clearer of what i understand

 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
webpage
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:maybe this will make it abit clearer of what i understand...

Yes, but what you understand doesn't produce what you want, so the question you need to answer is: Why not?

You have been given plenty of advice, but you seem to want someone else to do your work for you.

ShowSomeEffort (←click) or explain what it is you don't understand. It's a requirement of this website.

Winston
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
managed to get it working by using the if statement inside:

 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote:managed to get it working by using the if statement inside:

OK. Well done. Now I'd suggest that you look back over the advice that you've been given and see if you can do even better.
(Hint: you only need one 'if' statement)

Winston
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote: So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means

You start off with x set to 5 and print all the odd numbers less than 5
You then set x to 10 and print all the odd numbers less than 10
You then set x to 15 and print all the odd numbers less than 15

Do you see a pattern ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhradeep Banerjee wrote: So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means

Look at your latest code, and specifically at your 3 if statements. What is "the same" about them? They are basically doing the same thing.

Now look at Jan's statement again, and see if it makes more sense.

Winston

[Edit] too slow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic