提问人:Neon 提问时间:12/22/2022 最后编辑:Neon 更新时间:12/22/2022 访问量:26
如何检查是否输入了值,如果未输入任何值,请重新提示它们
How to check if value is entered, and if nothing is entered reprompt them
问:
我想使用 while 循环来检查用户是否输入了他们的猜测,如果未输入任何内容,请重新提示他们输入。我是 Java 新手,如果这真的很明显,很抱歉。
System.out.println("What is your guess? ");
guess = input.next();
我尝试使用 while(guess != null),但它没有用。
答:
0赞
Aashish Dalmia
12/22/2022
#1
import java.util.Scanner; // Import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("What is your guess? "); //
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}
0赞
Idle_Mind
12/22/2022
#2
使用循环:do...while
guess = "";
do {
System.out.print("What is your guess? ");
guess = input.next();
} while (guess.trim().length() == 0);
评论