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.
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
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./
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();