我想说我是Java新手,所以请不要因为我凌乱的代码而讨厌我。如果您不想在键盘上输入任何内容,则不会重复使用System.out.print("Enter your name")。循环循环,但此语句不重复。如果你输入一些字母,print语句就会被执行,我不知道为什么。这里有一张图片,让它更清晰。也许这是我犯的一个非常简单的错误,但我看不出来。感谢您的任何帮助或提示如何解决此问题。
它看起来是这样的:

这就是我想要的:

package loop;
import java.util.Scanner;
public class while_loop
{
public static void main(String[] args)
{
String word2 = null;
boolean T_F = true;
while (T_F == true)
{
System.out.print("Enter your name:");
Scanner in = new Scanner(System.in);
word2 = in.next().toUpperCase();
if (word2 != null && word2.length() > 3)
{
in.close();
T_F = false;
System.out.println("Done");
}
}
}
}嗯,好了,解决了。小小的错误。
word2 = in.next().toUpperCase();应该是
word2 = in.nextLine().toUpperCase();发布于 2016-03-22 04:22:22
你的if语句应该是:
if (!(word2 = in.nextLine().toUpperCase()).isEmpty() && word2.length() > 3){
in.close();
T_F = false;
} Scanner.next()将只返回空格之前的内容。
读取当前行后,Scanner.nextLine()将自动向下移动扫描仪。
https://stackoverflow.com/questions/36140529
复制相似问题