-2

我正在为我的 java 课做一些作业,我遇到了这个错误:

public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";

public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    System.out.println("Welcome to QuizApp! Programmed by Chris!");
    System.out.println("Please type the number corrisponding to the correct answer.");
    int score = 100;
    for(int i = 0;i <5;i++) {
        if (i == 0) {
            System.out.println("What is the capital of America? ");
            System.out.println("1) Washington D.C \t2) New York");
            System.out.println("3) Philadelphia \t4) Manhatten");
            int answer = myScanner.nextInt();
            if (answer == 1) {
                System.out.println(ANSI_GREEN+"Correct!");
            }else{
                System.out.println("Either your answer was wrong, or there was a syntax error.");
                score = score - 20;
            }
            if (i == 1) {
                System.out.println("What ocean do emporer penguins swim in? ");
                System.out.println("1) The Indian Ocean \t2) The Pacific Ocean");
                System.out.println("3) The Atlantic Ocean \t4) The Antarctic Ocean");
                answer = myScanner.nextInt();
                if (answer == 4); {
                    System.out.println(ANSI_GREEN+"Correct!");
                }else{
                    System.out.println(ANSI_RED+"Either your answer was wrong, or there was a syntax error.");
                }
            }           
        }   
    }
}

}

它说我的大括号有错误:

        if (i == 1) {
            System.out.println("What ocean do emporer penguins swim in? ");
            System.out.println("1) The Indian Ocean \t2) The Pacific Ocean");
            System.out.println("3) The Atlantic Ocean \t4) The Antarctic Ocean");
            answer = myScanner.nextInt();
            if (answer == 4); {
                System.out.println(ANSI_GREEN+"Correct!");
            }else{
                    System.out.println(ANSI_RED+"Either your answer was wrong, or there was a syntax error.");
            }
        }

我试图在 if 语句中放置一个 if-else 语句。我知道为什么它给我这个错误,但我不知道如何解决它。(错误很可能是 java 认为 }else{ 与第一个 if 语句配对。

4

1 回答 1

3

问题就在这里

if (answer == 4);

您的语句后有一个分号,if用于终止条件。删除它,你应该没问题。

于 2015-06-03T17:43:05.290 回答