• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a comparison program  RSS feed

 
Mike Arndt
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm trying to write a program that takes three values from the user, asks if he/she wants the largest or smallest number and then output said number. I also need a generic message to output if the numbers are the same. Any help is well appreciated, but please keep in mind that this is a entry level java course and we haven't covered much yet so I'm still using if statements and such.

I have most of it down, it works when I ask for the greatest value and each value is different, but anytime I ask for the smallest value it gives me the largest, also it makes me type "no" twice(when asking for lesser value) to actually output the value. The program does, however, give the correct response when the numbers are the same, whether I ask for the greater or lesser value(still makes me type "no" twice.) While debugging, I see that the program functions normally until it gets to the "if boolean is true/false" part and effectively treats the if statements as commands instead of variable checks(i.e, "if boolean = true" becomes "boolean = true") This may not be the case since I don't really know what I'm doing but that's what it seems to me.

This is what I have so far:




Thanks very much for any help anyone can provide!

Cheers!
MArndt
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you type "no", the first statement would read your choice and return false.
Then, it read your choice AGAIN and check.
In order not to read your choice every time, you should somehow remember the choice and compare against available options.

updown = true is an assignment, assign updown with value of true
updown == true is a comparison, check updown has value of true (some programming language use "=" for comparison as well, but not Java)
Actually you can even say
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (updown = true)
Because of this, you always get the maximum value.
 
Mike Arndt
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much guys! I can definitely see what you are talking about with the = vs ==. I can't believe I missed that! That part of the program works well now, but I am having trouble figuring out how to only ask for the yes or no once. I was under the impression that since the user didn't input "yes" it will bypass the first if statement and jump to else if and use the previous response to compare.



Is there a way where I can condense this into one if/else statement where the input comparison(yes vs no) happens in one line? Again, I appreciate any help you can offer./

Thanks again,
Mike
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can store the answer in a variable. And check that variable is equal to yes or no in if clause.

And here you have made another instance of Scanner class for check the choice. But it is not necessary. You can use the previous instance to get the choice.
But you have to remember one. After you have input the number, you have pressed enter key. Therefore there is a enter key after the numbers. Therefore you have to skip that enter key before getting the choice. You can do it by calling nextLine();

 
Mike Arndt
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ramesh! That makes more sense, and it all works now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!