如果答案是这个,那就不要问

时间:2018-10-09 17:11:53

标签: java if-statement java.util.scanner

因此,我想这样做,以便如果您对一个问题给出特定的答案,则跳过其他问题。 目前这是我的代码:


    System.out.print("How old are you? ");
    int age = Integer.parseInt(scanner.nextLine());
    System.out.print("Do you already have a scooter license? ");
    String scooter = scanner.nextLine();
    System.out.print("Do you already have a car license? ");
    String auto = scanner.nextLine();

    if (age >= 17 && auto.equals("no") || age >= 17 && auto.equals("No")) {
        System.out.println("You can get a car license.");
    } else if (age >= 17 && auto.equals("yes") || age >= 17 && auto.equals("Yes")){
        System.out.println("You can also buy a scooter.");
    } else if (age == 16 && scooter.equals("no") || age == 16 && scooter.equals("No")) {
        System.out.println("You can get a scooter license.");
    } else if (age == 16 && scooter.equals("yes") || age == 16 && scooter.equals("Yes")) {
        System.out.println("You can get a car license next year.");
    } else if (age < 16) {
        System.out.println("When you are 15.5 years old you can do your written exam for a scooter license.");
        System.out.println("And when you're 16 you can get a scooter license.");
    }
}

所以我想要的是,当您对第一个问题的回答不满16岁时,您跳过了另外两个问题。

如果您对第一个问题回答16,则跳过第三个问题。

如果您对第一个问题的回答为17或更高,则会跳过第二个问题。

你能帮我吗?

2 个答案:

答案 0 :(得分:2)

有一个简单的方法可以做到这一点。通过围绕其他问题添加简单的if语句。

System.out.print("How old are you? ");
int age = Integer.parseInt(scanner.nextLine());
if(age > 15)
{
    if(age == 16)
    {
        System.out.print("Do you already have a scooter license? ");
        String scooter = scanner.nextLine();
    }
    if(age > 16)
    {
        System.out.print("Do you already have a car license? ");
        String auto = scanner.nextLine();
     }
}

如果他们回答15或更少,则会跳过接下来的两个问题。 如果他们回答16,只会询问他们有关踏板车的信息。 如果他们的回答高于16,它将跳过踏板车并询问自动驾驶。

答案 1 :(得分:0)

首先-您可以使用toLowerCase()方法。任何答案都将以小写形式编辑,因此您无需检查答案中的大小写(等于“否”,等于“否”)。

您也可以使用returnSystem.exit(0);退出功能。

您还可以使用'? :'方式,以提高代码的可读性

    System.out.print("How old are you? ");
    int age = Integer.parseInt(scanner.nextLine());
    if (age < 16) {
        System.out.println("When you are 15.5 years old you can do your written exam for a scooter license.");
        System.out.println("And when you're 16 you can get a scooter license.");
        //return; / System.exit(0); - can use this way
    } else  if (age == 16) { //or this
        System.out.print("Do you already have a scooter license? ");
        String scooter = scanner.nextLine();
        System.out.println(scooter.toLowerCase().equals("no") ? 
                          "You can get a scooter license." //if yes
                          : "You can get a car license next year."); //if no
    } else {
        System.out.print("Do you already have a car license? ");
        String auto = scanner.nextLine();
        System.out.println(auto.toLowerCase().equals("no") ? 
                          "You can get a car license." 
                          : "You can also buy a scooter.");
    }
相关问题