0
How can I use 0==number%6 without changing the value of number in the following code?
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); if (0==number%6 || 0==number%4){ System.out.println("You won $50"); } else if (0==number%10 || 0==number%9){ System.out.println("You won $200"); } else { System.out.println("Try again"); }; } } Well I've tried number%6==0 as a condition but that doesn't work, and if I use 0==number%6 the value of number get changed so that all given inputs will give the same result and be true for the first if condition.
6 odpowiedzi
+ 1
I don't understand what you want to do. Can you explain further?
0
Okay the problem is that this is not working the way it should, it always says "You won $50"..
I want the three conditions to work, but it seems that if I write 0==number%6, it changes the value of number in a matter that this condition will be true always no matter the previous value of number, so whatever I put first will always be true, be 0==number%10 or 0=number%6, those will change the value of number to correspond with the first (if) condition and be true for it.
0
Or is it because of something else that I am not seeing?
0
try to put the conditions in parentheses.
if ((number%6==0) || (number%4==0)){
....
0
The first condition is true if input is a multiple of 6 or 4. If not, you check if it is a multiple of 10 or 9. If not "Try again".
40 --> 50$
100 --> 200$
4 --> try again
0
Yeah, it works, for some reason it does not in Sololearn (although it is a challenge), but I tried it now in eclipse and it does. It is just too simple for it not to work, I was kinda sad lol. Thank you anw