gpt4 book ai didi

Java bufferreader 在 ctrl z 上崩溃

转载 作者:行者123 更新时间:2023-12-01 13:58:48 27 4
gpt4 key购买 nike

我正在制作一个游戏,直到用户在命令行中输入退出为止。

用户可以输入不同的命令,例如 get 和 go,使用 get 命令,用户可以说出想要什么,获取棒球棒。我在代码中所做的就是拆分命令。

一切工作正常,但我发现了一个无法解决的错误。如果我输入“get”并按 space 然后按 ctrl+z 它会进入一个永远不会结束的 while 循环。

只有 ctrl+z 才会发生(1 次使用 ctrl c 但之后 1 次没有不再)

 private void run() 
{
while (! quitCommand)
{

String input = null;

try
{
input = null;
System.out.println("Input "+ input);
System.out.println("Give a command.");
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
input = is.readLine();
handleCommand(input);
// As long as the command isn’t to quit:
// get the next input line and handle it. (With handleCommand.)
}

catch (Exception e)
{
System.out.println("Something went wrong we are sorry try again.");
e.printStackTrace();
}

}
}

/**
* @param userInput (This is the entire input string from the user.)
*
* (Tell others to) Perform the task which belongs to the given
* command.
*/
private void handleCommand(String userInput)
{


// Split the user input string.
if (userInput != null) // user input can not be empty
{
String[] delenTekst = userInput.split(" ");

// The first word is a command. The rest is extra information
String command = delenTekst[0];
String extra = "";

for (int i = 1; i < delenTekst.length; i ++)
{
if (i == 1)
{
extra = extra + delenTekst[i];
}
else
{
extra = extra +" " + delenTekst[i];
}
}

switch (command)
{
// Check if the command is to travel between rooms. If so, handle
case "go"
:
this.checkRoomTravel(extra);
break;
// If there isn't any room travel, then check all other command
case "get"
:
System.out.println("Looking for " +extra );
this.handleGetCommand(extra);
break;
case "quit"
:
quitCommand = true;
break;
default
:
System.out.println("Command is not known try help for information");
break;
}
}
else
{
userInput = "help";
}
}

我是java新手,所以它可能非常简单。

在我的脚本顶部,我有一个私有(private) boolean 值quitCommand = false;,用于检查用户是否输入退出。

最佳答案

Ctrl+Z 关闭控制台,因此您的 readLine() 返回 null,假装表示已到达文件末尾。因此,您需要做的就是检查 readLine() 返回的 null 并在处理“退出”时处理它。

我更改了您的代码(只是为了测试我的论文),并且还简化了一些内容,例如您不需要在每次读取一行时重新创建一个 BufferedReader。

private boolean quitCommand = false;
private void runIt() {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
String input = null;

while(!quitCommand) {
try {
System.out.print("Give a command: ");
input = is.readLine();

// As long as the command isn’t to quit:
if(input == null || "quit".equals(input.trim())) quitCommand = true;
if(quitCommand) break;

// get the next input line and handle it. (With handleCommand.)
String[] words = input.trim().split("\\s+");

// ** This is the original handleCommand line **
System.out.println(input + ":" + Arrays.toString(words));
}
catch (Exception e) {
System.out.println("Something went wrong we are sorry try again.");
e.printStackTrace();
}
}
}

顺便说一句:要将输入拆分为单词,我将使用代码中所示的正则表达式。如果用户输入制表符或多个空格,这也适用。

关于Java bufferreader 在 ctrl z 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19467424/

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