【问题标题】:Can if statements be nested in a while loop?if语句可以嵌套在while循环中吗?
【发布时间】:2013-08-18 10:05:14
【问题描述】:

我一直在处理使用 do-while 循环的代码,我想在该循环中添加一个 if else 语句。 do-while 循环检查用户输入的文本,并在输入单词“exit”时结束。

public static void main(String[] args) {

    String endProgram = "exit";
    String userInput;
    java.util.Scanner input = new java.util.Scanner(System.in);

    do {

        userInput = input.nextLine();
        if ( !userInput.equalsIgnoreCase(endProgram) ) {
            System.out.printf("You entered the following: %s", userInput);
        } else

    } while ( !userInput.equalsIgnoreCase(endProgram) );

}

当我尝试编译此代码时,我从命令提示符处收到错误消息:

SentinelExample.java:20: error: illegal start of expression
            } while ( !userInput.equalsIgnoreCase(endProgram) );
            ^
SentinelExample.java:22: error: while expected
      }
       ^
SentinelExample.java:24: error: reached end of file while parsing
}
 ^

当我删除循环中的 if else 语句时,程序编译得很好。我的程序中的语法有问题吗?还是不能将 if else 语句放在 while 循环中?

【问题讨论】:

  • if语句可以嵌套在while循环中吗?可以
  • 你那里有一个无用的 else 关键字
  • 嘿,谢谢大家的帮助。只是想知道,为什么我的问题被否决了?
  • @KylerS 无意冒犯你,但有时非常简单的问题会被否决,通常我会尝试对我回答的问题投赞成票
  • @aaronman 好的,嘿,没人接。我是新手

标签: java if-statement while-loop do-while


【解决方案1】:

在此处检查您的代码,您缺少else 块:

    userInput = input.nextLine();
    if ( !userInput.equalsIgnoreCase(endProgram) ) {
        System.out.printf("You entered the following: %s", userInput);
    } else 

这是编译错误的原因。您可以通过完全删除 else 来解决此问题,或者如果您想在其中添加一些内容,请执行以下操作:

    userInput = input.nextLine();
    if ( !userInput.equalsIgnoreCase(endProgram) ) {
        System.out.printf("You entered the following: %s", userInput);
    } else {
        // Do something
    }

为了回答您的问题,是的,将 if 语句嵌套在 while 循环中是完全有效的。

【讨论】:

    【解决方案2】:

    您缺少 {} 来关闭 else 块。而不是

    do{
      if(){
      }else
    }while()
    

    使用

    do{
      if(){
      }else{
      }
    }while()
    

    【讨论】:

      【解决方案3】:

      那是因为您的 else 语句只会让编译器认为 while 条件处于 else 条件下并且是错误的。 删除它

      【讨论】:

        【解决方案4】:

        很有可能:

        public static void main(String[] args) {
        
            String endProgram = "exit";
            String userInput;
            java.util.Scanner input = new java.util.Scanner(System.in);
        
            do {
        
                userInput = input.nextLine();
                if ( !userInput.equalsIgnoreCase(endProgram) ) {
                    System.out.printf("You entered the following:"+userInput);// use plus ssign instead of %s
                }
        
            } while ( !userInput.equalsIgnoreCase(endProgram) );
        
        }
        

        【讨论】:

        • @SusanYanders:预期的输出是什么?
        【解决方案5】:
        package second;
        
        public class Even {
        
            public static void main(String[] args) {
        
                int a=0;
                while (a<=100)
                {
                    if (a%2==0)
                    {
                        System.out.println("The no is EVEN : " + a);
                    }
                    else 
                    {
                    System.out.println("The num is ODD : " + a);    
                    }
        
                } } }
        

        为什么这不能正常工作,它只显示0,甚至!

        【讨论】:

        • 因为你使用了while语句错误。你想做的事情应该用一个for循环来完成,比如“for (int a = 0; a
        猜你喜欢
        • 1970-01-01
        • 2016-10-30
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2017-02-06
        • 2020-06-22
        • 2015-12-28
        • 1970-01-01
        相关资源
        最近更新 更多