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

标签 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 if。这似乎与此相矛盾。

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

最佳答案

如果语句非常简单,您遇到的问题也非常简单。当你这样做时

if(cond1){
    code1
}

if(cond2){
    code2
}else{
    code3
}

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

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

例如

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

如果所有 if 语句的计算结果都为 false,则只会运行最后一个 else 语句。

或者,您可以使用 switch 语句,这些语句可能更令人困惑,有时更强大,所以我将链接到它并让您阅读它。 https://www.w3schools.com/java/java_switch.asp

关于Java:为什么 else 语句总是在我的 while 循环中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56997811/

相关文章:

if-statement - 如何在jmeter的if Controller 中使用属性变量

R,将 dplyr::mutate 与包含 grepl() 的 ifelse 一起使用会产生意外结果

java - 创建了一个自定义 Spring boot layoutFactory,现在无法启动我的 Spring Boot 应用程序

java - 将 JSON unicode 字符转换为 unicode 值

java - 如何设置 ImageView 占据屏幕的宽度?

java - 扫描仪在 if 语句中不起作用

Java Thread 和 class.loader/class.forname 有助于理解

java - 使用嵌入式容器测试 EJB。我怎样才能登录?

python - Python评估和逻辑运算符

javascript - 在 JavaScript 中,如何以简单的方式检查 "a"与 "b," "c,"等?