I have a simple guess number game. It has a function to ask whether you want tips. It saves the response in a boolean
called tips
as shown.
while (run) {
while (tinvalidrun){
System.out.println("Do you want any tips? y or n?");
input=in.next();
switch(input){
case "y":
System.out.println("Ok, we will tell you how close you are!");
tinvalidrun=false;
tips=true;
break;
case "n":
System.out.println("Wanna go hard eh? Well, go along!");
tinvalidrun=false;
break;
default:
System.out.println("You pressed the wrong key!\nDo you have butter fingers?");
}
}
Then I have some more code that makes more variables and makes a random number. And finally I get the user input for the guess and test it:
do {
System.out.println("Enter a number: ");
guess = in.nextByte();
times++;
if (guess == gval) {
break;
} else {
System.out.println("No luck! Try again!");
if (tips=true) {
if (guess < gval) {
System.out.println("You are too low!");
}
else {
System.out.println("You are too high!");
}
}
}
I tried putting the tips=true
and guess < gval
in one if
with &&
but it doesn't work. I did this:
if(guess<gval && tips=true)
It asks me to replace =
with <=
or something like that. I tried using two if
statements but when you say no, it still shows the tips. I tried looking at the brackets but they look fine. Please Help! If there is any simplification or improvements or ideas I can do to my code, they are welcome.
I have more question as well. It crashes (obviously) when you try to type a letter to the integer. I tried using strings and my tinvalidrun
loop to avoid this but I can't. Is there a way to do this?
Thanks! :)
Another way to get an Excel IF to test multiple conditions is by using an array formula. To complete an array formula correctly, press the Ctrl + Shift + Enter keys together. In Excel 365 and Excel 2021, this also works as a regular formula due to support for dynamic arrays.
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)
A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
You need tips == true
, or just if (guess<gval && tips)
and if (tips)
as you are testing a boolean
.
=
is an assignment operator, ==
is an equality operator. It is easy to mix the two up, but the difference is enormous...
You say "I tried using two if statements but when you say no, it still shows the tips.". The if
statement expects an expression which must have type boolean
. Any assignment expression (i.e. tips = true
) evaluates as the new value assigned to the variable. From the JLS §15.26.1 Simple Assignment Operator:
At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.
Therefore if (tips = true)
is valid syntax, because tips = true
is both an assignment and also a boolean
expression which can therefore be used in an if
.
In your case tips = true
assigns true
to tips
(even if it started out false
), then returns that new value of tips
. The if
sees the new value (true
) and continues happily on. It is not therefore a test that tips
was originally true
. By example, this means that the following prints out the "Oops!"
text:
boolean tips = false;
if (tips=true){
System.out.println("Oops! This tests as true, but that isn't what we wanted");
}
As you can see, conflating assignment and testing is generally a bad idea. It is usually a mistake if you use =
in an if
/do
/while
expression. Sometimes it can help with brevity but it is generally bad practice.
if (guess<gval && tips = true)
work?This is due to operator precedence. In the expression if (guess<gval && tips = true)
the operator precedence is <
then &&
and then =
, so the compiler no longer evaluates the tips = true
part as an assignment of true
to tips. You could use brackets to contain tips = true
, but as I said above you don't really want the assignment so you can ignore this detail †.
For the last part of your question - you say you get an invalid byte if someone inputs a letter. I'm guessing you get a java.util.InputMismatchException
.
You can use Scanner.hasNextByte()
to test if the input is a byte before consuming it. If that returns false you can consume and discard the input using nextLine()
, print out an error message and loop again.
†Depending on how you were writing your code you may also have seen a message from eclipse such as "The operator <= is undefined for the argument type(s) boolean, Boolean" or from javac such as "error: unexpected type ... required: variable, found: value". None of them tell you much other than your syntax is sufficiently messed up the compiler can't parse it.
if(tips = true)
is always true
condition because here we have assigned value true
to tips
.
It will first assign tips to true and then check will be performed in if
so your else
part is dead due to this code and control will never ever enter in else
part.
If you want to check whether tips
is true
or not then you can just write if(tips)
and if you want to check whether it is false
than you should write if(tips == false
). Where ==
will check equality while =
will assign the value or reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With