gpt4 book ai didi

java - 制作命令行,如果语句不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 04:01:57 24 4
gpt4 key购买 nike

我想制作一个命令行,只是为了运行基本命令。到目前为止,我已经做到了让人们可以告诉程序他们的名字。但是,当我不输入名称时,它会像我输入一样对待它。这是我的类(class):

public static void main(String args[])
throws IOException
{
int a = 1;

do
{
System.out.print("$$: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String wtt = null; // wtt = what they typed!
wtt = br.readLine();

if(wtt == null)
{
System.out.println("Why wont you tell me your name!");
}
else
{

System.out.println("Thanks for the name, " + wtt);
}

}
while(a == 1);
}

这是输出

$$: well

Thanks for the name, well

$$: hole

Thanks for the name, hole

$$:

Thanks for the name,

为什么不起作用?

最佳答案

调用 readLine() on a BufferedReader只会在输入结束时返回 null。在这里,输入还没有结束,你只是输入了一个空行,所以 ""(空字符串)就是结果。

您需要结束输入流,通常使用 Ctrl-C。然后你会得到 “你为什么不告诉我你的名字!”。但随后您需要跳出无限循环。

关于java - 制作命令行,如果语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com