3
Scanner input = new Scanner(System.in);
String str = input.nextInt();
while ( (str.nextInt() != 5) || (str.nextInt() != 10) ){
str = input.nextInt()
}
The code should invoke the method nextInt()
every time the user typed some number other than 5 or 10. What’s happening here is that when I type 5 or 10 it keeps repeating, invoking the method nextInt()
.
Welcome to Sopt. What do you want with this comparison? Edit the question and explain better what this code does or should do.
– user28595
There is nothing wrong with the fact that we do not know what is expected ;). A brief description of what is wrong or from the input which is the result obtained and which is the expected amount already helps a lot!
– rray
The code should invoke the nextInt() method every time the user enters a number other than 5 or 10. What’s happening here is that when I type 5 or 10 it keeps repeating, invoking the nextInt method().
– Marcelo
So it’s working perfectly. If it’s to continue if it’s different from 5, OR if it’s different from 10, it’s right.
– Bacco
Tip: What you want is to be executed when the number is "other than 5 and 10" and not "different from 5 or of 10"
– ramaral
For you to understand better: If it’s 5, it’s different from 10, then go on. If it’s 10, it’s different from 5. So go on. That’s what you determined with this ||.
– Bacco
Try it like this
while ( (str.nextInt() != 5) && (str.nextInt() != 10) )
– Bacco
Ow!!! Thank you, you solved my problem, but I’m still confused about the "e" and "ou". My logic is horrible. I’m sorry about the misspelled question
– Marcelo
What confuses a little is the fact of using NOT
!
, because the logic reverses. Another way of writing is this:while ( !( str.nextInt() == 5 || str.nextInt() == 10 ) )
- "Until it is 5 OR 10, run" - We are using an OR within parentheses, and denying the result of OR as a whole.– Bacco
Do this exercise: In the sentence "3 is different from 5 ? different from 10" what article(E/OU) you need to use to make sense of it?
– ramaral
Hey, Massa, thanks for the help you guys gave me. In this, I ended up remembering the truth table in the subject of logic in mathematics, which helped me understand better, too. V and V = VV and F = FF and V = FF and F = F-----------------V or V = VV or F = V --> so the loop was never false. F or V = VF or F = Ftem others too, which I have forgotten now, like the "what if", "implies" and others, but I will take a look later. Thanks, guys, you’re great.
– Marcelo