'How to answer with (y,Y OR n,N) on math expression with random num and operators
The question: is n1>n2 OR n1<n2 The answer: y,Y OR n,N The num + operators must be random. If the user wrong he can try again - one more time. So far I get to that point but Im stuck. I need help with my code, or another way that will be easier for me to answer that.
public static void code1() {
Scanner s = new Scanner(System.in);
Random r = new Random();
int n1 = r.nextInt(10) + 1;
int n2 = r.nextInt(10) + 1;
int max = Math.max(n1, n2);
int min = Math.min(n1, n2);
boolean biggerThan = n1 % 2 == 0; //if this value is true it will print >
boolean smallerThan = n1 % 2 !=0;
System.out.print(n1);
if (biggerThan)
System.out.print(">");
if (smallerThan)
System.out.print("<");
System.out.println(n2);
char answer = s.next().charAt(0);
if(answer=='y'||answer=='Y'){
if(biggerThan) {
if (((max = n1) > (min = n2)))
System.out.println("Well done");
else if (((min = n1) > (max = n2))) //
System.out.println("Wrong answer");
}
if(smallerThan){
if (((max = n1) < (min = n2)))
System.out.println("Wrong answer");
else if (((min = n1) < (max = n2))) //
System.out.println("Well done");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
