• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does my 'd' variable stay at zero?

 
Alan Goodoien
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I am definitely new to programming any language. I am apologizing in advance. But I am trying to create a program that takes 3 numbers from keyboard input and then displays them from lowest to highest. This compiles and runs but d is staying at zero(I think). It correctly orders the 'e' and 'f' no matter the order you enter numbers. I am stumped please help.



[HENRY: Added code tags]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of d only gets changed inside your while loop and you will only enter the while loop if one of d, e or f is not zero. As you initialise them all to zero before the loop, the code in the loop will never be run.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

And welcome to JavaRanch!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't think we're beating up on you, but I'd like to make another suggestion. always always ALWAYS use braces. Some day, you're going to revisit some code and thing "I need to do something else here, like a System.out.println()". You'll stick it in, and things will REALLY go crazy because something won't be in the loop anymore. It really doesn't cost that much to always have them, and personally, I find code much easier to read with them in there.

Indentation alone isn't enough to see what is part of what. in your code below, your "if" on line 33 in indented more than your 'while' on line 20, but less than the if on line 23. it's confusing as to whether this should be inside the loop or not.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Don't think we're beating up on you, but I'd like to make another suggestion. always always ALWAYS use braces. Some day, you're going to revisit some code and thing "I need to do something else here, like a System.out.println()". You'll stick it in, and things will REALLY go crazy because something won't be in the loop anymore. It really doesn't cost that much to always have them, and personally, I find code much easier to read with them in there.

Indentation alone isn't enough to see what is part of what. in your code below, your "if" on line 33 in indented more than your 'while' on line 20, but less than the if on line 23. it's confusing as to whether this should be inside the loop or not.


+1 on this. Braces are a coder's best friend, in my opinion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic