aspose file tools*
The moose likes Beginning Java and the fly likes Why does my 'd' variable stay at zero? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does my Watch "Why does my New topic
Author

Why does my 'd' variable stay at zero?

Alan Goodoien
Greenhorn

Joined: Feb 19, 2010
Posts: 1



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

Joined: Aug 05, 2005
Posts: 3566
    
  15
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.


Joanne
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 02, 2003
Posts: 11317
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does my 'd' variable stay at zero?