为什么我的代码无法正常工作?

时间:2016-10-13 19:56:21

标签: java

我对编码有点新意。所以,我想知道为什么我的代码不能正常工作?如果您运行代码,它会询问您正确的值,然后打印出来是否正确。但它也打印出“其他”声明。

package MyFirstProject;
import java.util.Scanner;

public class MyFirstProject {

    public static void main(String[] args) {
        int answer;
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is 5 + 5");
        answer = scanner.nextInt();

        if(answer == 10){
            System.out.println("You are correct!");
        }if(answer == 9){
            System.out.println("You were close! Try again!");
        }if(answer == 11){
            System.out.println("You were close! Try again!");
        }
        else{
            System.out.println("You are wrong! Try again!");
        }

    }
}

5 个答案:

答案 0 :(得分:0)

问题在于if语句。 Else与last if语句相关联。每当答案值不是" 11"时,它将每次触发。如果"你需要使用"否则如下图所示。

 if(answer == 10){
        System.out.println("You are correct!");
    }else if(answer == 9){
        System.out.println("You were close! Try again!");
    }else if(answer == 11){
        System.out.println("You were close! Try again!");
    }
    else{
        System.out.println("You are wrong! Try again!");
    }

答案 1 :(得分:0)

使用其他

public static void main(String[] args) {
    int answer;
    Scanner scanner = new Scanner(System.in);
    System.out.println("What is 5 + 5");
    answer = scanner.nextInt();

    if(answer == 10){
        System.out.println("You are correct!");
    }else if(answer == 9){
        System.out.println("You were close! Try again!");
    }else if(answer == 11){
        System.out.println("You were close! Try again!");
    }
    else{
        System.out.println("You are wrong! Try again!");
    }


}

答案 2 :(得分:0)

原因是你在第二和第三IF之前缺少ELSE。

if(answer == 10){
    System.out.println("You are correct!");
}else if(answer == 9){
    System.out.println("You were close! Try again!");
}else if(answer == 11){
    System.out.println("You were close! Try again!");
}
else{
    System.out.println("You are wrong! Try again!");
}

在你的情况下,第三个IF不是真的所以执行了其他的。使用我的修复和10作为输入值,首先IF将为真,然后所有剩余的将不会被执行。

答案 3 :(得分:0)

你需要用if替换if if:

switch(answer):
   case 10:
      System.out.println("You are correct!");
      break;
   case 9:
   case 11:
        System.out.println("You were close! Try again!");
        break;
   default:
        System.out.println("You are wrong! Try again!");

在您的示例中,else与最后一个if语句匹配,因此它不是“最后的手段”选择。您需要将所有ifs抓取到一个链中,以便else匹配所有非if匹配的情况。

或者使用开关:

$ python web2py.py -S appname -M
.....
>>> db = DAL('mysql://user:password@localhost/db_name')

答案 4 :(得分:0)

原因是,例如

if(answer == 10){
    System.out.println("You are correct!");
} else if(answer == 9){
    System.out.println("You were close! Try again!");
} else if(answer == 11){
    System.out.println("You were close! Try again!");
} else{
    System.out.println("You are wrong! Try again!");
}

所以正确的方法是确保它只检查一个块并打印出正确的信息,这就是你所做的:

class MyApp extends Application
{           
    public static MyApp me;
    public MyApp()
    {
        me=this;
    }

    ... 
}