+ 2
Boolean and conditional operator
Hi! I have a code that works perfectly and do what it suppose to do, but the problem is, that it requires a boolean to be assigned to user's input and a conditional operator in println(). Many points were deducted from my grade for this, and I'm trying to figure out, how and where I was supposed to put boolean and for what purpose I have to use a conditional operator. My code prompts user to enter an even multiple of 13 more than 100 and evaluates it. Any ideas? Here is my code: https://code.sololearn.com/c1bIG5ws8cB4 Thank you in advance!
6 Antworten
+ 3
you could do something like:
int input = kbdIn.nextInt();
boolean isEvanMultiple = (input % 2 == 0 && input % 13 == 0 && input ≥ 100);
System.out.println(isEvanMultiple ? "other factor is " + input/13.0 : "that's not a multiple of 13 above 100");
+ 1
I'm not sure exactly, something you could do is
boolean isEven = kbdIn.nextDouble() % 2 == 0;
and then the of statement would read
if (isEven) // code...
something along those lines. Obviously add more conditions to fulfil your criteria. Careful though, because this way you won't have access to the actual number, so be sure to store it first.
On another note, your code says it's looking for an even multiple of 13 but the if statement has all OR operators, so if any one condition is true the statement will pass - try entering 2 or 13. These should be && based on your comments.
+ 1
there is a boolean in what i wrote
0
Great answer! Thank you very much! Just corrected ||.
If you have a minute, can you please try to add boolean and cond. operator to my code, because I'm fighting with it entire day and can't figure out how to do it. I know it's a easy/silly stuff, but I'm a beginner and need some help from experienced coders. Thanks again!
0
Thank you, Jeremy! Good idea to remove one variable, but I still can't fit there a boolean. Just updated my code
0
I see, but I can't fit it in the code properly