public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
System.out.println("Enter your name: ");
String firstname =Keyboard.nextLine();
System.out.println("Welcome "+ firstname+ "!"+ " Please answer the following questions:");
int x = (int)(20 * Math.random()) + 1;
int y = (int)(20 * Math.random()) + 1;
int sum = (x+y);
System.out.println(x + " + " + y + " = ");
String answer =Keyboard.nextLine();
if (sum == (x+y)){
System.out.println("Correct!");
}else if(sum != (x+y))
System.out.println("Wrong!");
我必须做一个简短的测验,但我不能让输出说错。我可以说它是正确的,但没有错。
答案 0 :(得分:1)
sum
,因此 x+y
始终等于int sum = (x+y);
。
您可能希望将answer
解析为int
并检查它是否等于answer
。
if (sum == Integer.parseInt(answer)) {
System.out.println("Correct!");
} else {
System.out.println("Wrong!");
}