我的if和else编码不起作用

时间:2017-09-28 19:51:54

标签: java

首先,我是这个网站和编码的新手所以请原谅我的菜鸟错误,其次我的问题是,我打算通过使用java编码的eclipse软件进行测验(显然)。但每次我运行它,即使我输入正确的答案,它反应就像我输入了错误的答案。你能指出我的错误吗?

import java.util.*;

public class Quiz {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    @SuppressWarnings("resource")
    Scanner in = new Scanner(System.in);
    System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
    String userName = in.nextLine();
    System.out.println("Welcome " + userName + "! Let's start! Your first question is what is the capital of Norway?");
    String answer = in.nextLine();
    if (answer == ("Oslo"))
    System.out.println("Good Job! Your next question is how many states are there in U.S.A.?");

else        
{
    System.out.println("You've failed!");
}
    {

        @SuppressWarnings("unused")
    String answer1 = in.nextLine();
    if (answer1 == ("50"))
    System.out.println("Well done! Your next question is where did the first football world cup happen?");
    else        
    {
        System.out.println("You've failed!");
    }
    String answer2 = in.nextLine();
    if (answer2 == ("Uruguay"))
    System.out.println("Well done! Your next question is");
    else        
    {
        System.out.println("You've failed!");
    }
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

它与比较字符串有关。

当您将if条件更改为:

时,应解决您的情况
 if (answer.equals("Oslo")) { // Do something } 

使用equals()时,比较您尝试比较的内容。 如果您使用==它实际上会比较您尝试比较的参考。因此,对象存储(回答)给定字符串" Oslo",这是不相等的,因此返回false。

除了您在评论中提出的问题

要获得快速解决方案,您可以使用嵌套的if-else语句。

您还可以为每个问题使用新的if语句,彼此相邻。这将需要更多的代码重写。参见示例:

public class App {

   public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
        String userName = in.nextLine();
        System.out.println("Welcome " + userName + "! Let's start! Your first question is what is the capital of Norway?");
        String answer = in.nextLine();
        if (!answer.equals("Oslo")) {
            System.out.println("You've failed!");
        }

        System.out.println("Good Job! Your next question is how many states are there in U.S.A.?");
        answer = in.nextLine();
        if (!answer.equals("50")) {
            System.out.println("You've failed!");
        }
        System.out.println("Well done! Your next question is where did the first football world cup happen?");
    }
}

但是,正如您所看到的,代码变得非常重复,因此使用函数将是一个很好的下一步。见下一个例子:

public class AppWithFunction {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
        String userName = in.nextLine();
        System.out.println("Welcome " + userName + "! Let's start!");

        handleQuestion(in, "Your first question is what is the capital of Norway?", "Oslo");
        handleQuestion(in, "Your next question is how many states are there in U.S.A.?", "50");
}

private static void handleQuestion(Scanner in, String questions, String answer) {
    System.out.println(questions);
         if (!in.nextLine().equals(answer)) {
            System.out.println("You've failed!");
        }
    }
}

目前可能有点超出你的范围,但是如果你有一堆问题,将它们存储在地图中(question = key,answer = value),迭代它们并相应地处理它们将是这是一个很好的下一步。(也许以后可能是一个很好的自我指定的新练习;)。)。