Java:为什么其他语句总是在我的while循环中运行?

时间:2019-07-11 22:00:01

标签: java if-statement

我在用Java编程时注意到一个问题。自从我迷上Java以来​​已经有6年了(我一直在进行前端设计和开发,自高中以来就不需要使用Java进行编程)。我试图刷新思路,进行一些面向对象的编程,遇到了一个我从未见过的问题。

我试图建立学校数据库(更具体地说是一个简单的界面),即使if语句通过,我的else语句也总是运行。 任何人都可以向我解释为何if语句通过时else语句仍会运行吗?

    else { 
System.out.println("Bart, come to Principal Skinner's office immediately. You broke the system. \n"); 
}

直到我将else语句更改为else if语句(以明确区分这些if语句)之前,我无法修复此问题。

    else if(!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))
      {
        System.out.println("Bart, come to Principal Skinner's office immediately. You broke the system. \n");
      }

代码如下:

Scanner scanner = new Scanner(System.in);
    int end = 0;
    while(end == 0)
    {
      System.out.println("Welcome to Springfield Elementary School");
      System.out.println("----------------------------------------");
      System.out.println("Please select from the following options");
      System.out.println("1) Add Course");
      System.out.println("2) Remove Course");
      System.out.println("3) View All Courses");
      System.out.println("4) Exit");
      System.out.print("-->");
      String input = scanner.nextLine();
      if(input.equals("1")) 
      {
        System.out.println("That function is currently unavailable at this time");
      }
      if(input.equals("2")) 
      {
        System.out.println("That function is currently unavailable at this time");
      }
      if(input.equals("3")) 
      {
        System.out.println("That function is currently unavailable at this time");
      }
      if(input.equals("4")) 
      {
        end = 1;
        System.out.println("Thanks for accessing the Springfield Elementary School Database. Have a nice day.");

      }
      else { 
        System.out.println("Bart, come to Principal Skinner's office immediately. You broke the system. \n"); 
      }
   }

我不是真的对这是否起作用感兴趣,但是为什么其他的如果起作用而else语句却不起作用。这不是为了学校或工作,而是为了纯粹的学习。根据我对if语句的理解,如果它们通过了,则应该跳过所有其他条件语句,除非它是else if。这似乎与之矛盾。

为什么我的else语句总是在while循环内运行?

2 个答案:

答案 0 :(得分:3)

如果语句非常简单,并且遇到的问题也非常简单。当你做

if(cond1){
    code1
}

if(cond2){
    code2
}else{
    code3
}

它评估,如果cond1为true,则运行cond1。然后执行:如果cond2为true,则运行code2,否则(否则)运行code3。

您将所有if语句分隔开,因此else应用于的唯一一个就是最后一个。您正在寻找的是其他情况。

例如

if(cond1){
    code1
}else if(cond2){
    code2
}else{
    code3
}

仅当您所有if语句的计算结果都为false时,才会运行最后一个else语句。

或者,您可以使用switch语句,这些语句可能会更加混乱,有时甚至更强大,因此,我将仅链接至该语句并让您对其进行阅读。 https://www.w3schools.com/java/java_switch.asp

答案 1 :(得分:1)

else仅适用于最后一个if语句(检查"4"的语句),如果您不想检查其他条件,则一旦true ,请使用switch或在continue;内添加if。 即:

if(input.equals("1")) {
  System.out.println("That function is currently unavailable at this time");  
  continue;
}
if(input.equals("2")) {
  System.out.println("That function is currently unavailable at this time");  
  continue;
}
...

开关示例:

Scanner scanner = new Scanner(System.in);
    int end = 0;
    while(end == 0)
    {
      System.out.println("Welcome to Springfield Elementary School");
      System.out.println("----------------------------------------");
      System.out.println("Please select from the following options");
      System.out.println("1) Add Course");
      System.out.println("2) Remove Course");
      System.out.println("3) View All Courses");
      System.out.println("4) Exit");
      System.out.print("-->");
      String input = scanner.nextLine();
      switch(input) {
        case "1":
        case "2":
        case "3":
           System.out.println("That function is currently unavailable at this time");
           break;
        case "4":
           end = 1;
           System.out.println("Thanks for accessing the Springfield Elementary School Database. Have a nice day.");
           break;
        deafult:
           System.out.println("Bart, come to Principal Skinner's office immediately. You broke the system. \n");
      }
    }