我遇到的問題是,在正確的答案後,它仍然問這個問題。我錯過了什麼?我現在對JAVA仍然很陌生,不想因爲可能是一個簡單的錯誤或疏忽而感到氣餒。數學加碼,試圖在正確答案後結束
package acourtney;
import java.util.Scanner;
public class Hmwk03 {
public static void main(String[] args) {
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
int counter = 0;
Scanner input =new Scanner(System.in);
System.out.print ("What is " + number1 + " + " + number2 + " ? " + "(You have three chances) ");
int answer = input.nextInt();
if (number1 + number2 == answer)
System.out.println("Answer is correct");
else
System.out.println("Your answer is incorrect. You have two chances left ");
counter = 2;
System.out.print("What is " + number1 + " + " + number2 + " ? ");
answer = input.nextInt();
if (number1 + number2 == answer)
System.out.println("Answer is correct");
else
System.out.println("Your answer is incorrect. You have one chance left");
counter = 3;
System.out.print("What is " + number1 + " + " + number2 + " ? ");
answer = input.nextInt();
if (number1 + number2 == answer)
System.out.println("Answer is correct");
else
System.out.println("Your answer is incorrect, You have failed");
}
}
你看過關於在Java中猜測數字的其他問題嗎?或者你是否至少用調試器遍歷了你的代碼? –
總是加上大括號'{}'。不要因爲沒有必要就忽視它們。 – Carcigenicate
@Carcigenicate這是一種個人風格的選擇。 – weston