Java:如果变量为空(来自用户输入);休息; 否则继续main方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19233475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:17:58  来源:igfitidea点击:

Java: If variable is empty (from user input); break; else continue the main method

javaloopsbreak

提问by jtaylor94

sorry if this is a silly question but I am looking for a way to break and start another iteration of the loop if the user's input is null. Here's my code:

对不起,如果这是一个愚蠢的问题,但我正在寻找一种方法,如果用户的输入为空,则中断并开始循环的另一次迭代。这是我的代码:

while(true) {
    // Get the users input
    Scanner Input = new Scanner(System.in);
    System.out.println("Enter text:");
    String studentInfo = Input.nextLine();

    if (studentInfo == null) {
        System.out.println("Please enter valid infomation.");
        break;
    }
    continue;
}

After the break, I want it to go back to the while(true) and start again from there, by asking the user for an input again.

休息后,我希望它返回到 while(true) 并从那里重新开始,通过再次要求用户输入。

Thanks guys,

谢谢你们,

JT

JT

采纳答案by Mena

Check for:

检查:

if (studentInfo.isEmpty()) {
    continue;
}

Remove the Scanner input = new Scanner(System.in);declaration from inside the whileloop.

Scanner input = new Scanner(System.in);while循环内部删除声明。