gpt4 book ai didi

java - 接受用户输入,直到输入 <>

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:49 25 4
gpt4 key购买 nike

我首先要说的是,我已经查看了名为“我必须制作一个循环以获取用户输入,直到输入“完成””的线程,我对那里给出的代码答案没有运气。

我对编辑命令的描述是这样的:

"Edits a text file if exists. Otherwise, creates new text file. The command waits for the user to type in text(must support multiple lines of text). The user ends the input by typing <<EOF>> and hitting enter."

现在我的代码是这样的:

else if (spaceSplit[0].equals("edit")) {
String name = spaceSplit[1];
boolean endOfFile = false;
String content = "";

while(endOfFile == false){
String userInput = s.next();
content += userInput;
if(content.contains("<<EOF>>")){
endOfFile = true;
}
}

FileSystem.edit(name, content);


}

没有任何错误,但我的else声明打印。我的else声明代码是这样的:

else {
System.out.println("That is not a command. Please try again.");
}

同样有趣的是,该程序贯穿了整个 do while loop然后打印其他。我知道这一点是因为确切打印的是:$That is not a commond. Please try again.

这是我的do while loop的开始:

do {

System.out.print("$");

String input = s.nextLine();
input = input.toLowerCase();
spaceSplit = input.split(" ");

相当困惑。还有我的edit(String name, String content)功能如下:

public static void edit(String name, String content){
for(int i = 0; i < texts.size(); i++){
if(texts.get(i).getName().equals(name)){
texts.get(i).setContent(content);
} else {
texts.add(new TextFile(name,content));

for(int j = 0; j < directories.size(); j++){
if(directories.get(j).getName().equals(wDir.getName())){
texts.get(texts.size() - 1).setParent(directories.get(j));
System.out.println("The parent of " + name + " is " + directories.get(j).getName());
}
}
}
}
}

如您所见,我已在 edit(name,content) 的末尾进行了检查。方法通过打印文本文件的父目录来检查文件是否正确创建。

这就是我调用编辑命令后我的程序应该如何运行:

$mkdir d
$cd d
$edit stuff.txt
Hello everyone, this is just an example!<<EOF>>
The parent of stuff.txt is d
$exit
Good Bye!

如果您提供任何帮助,我们将不胜感激。

这是全部do while loop :

do {

System.out.print("$");

String input = s.nextLine();
input = input.toLowerCase();
spaceSplit = input.split(" ");

if (spaceSplit[0].equals("mkdir")) {
if (spaceSplit[1].equals("-p")) {
for (int i = 3; i < spaceSplit.length; i++) {

}
} else if (spaceSplit[1].contains("/")){
//This method will create a directory farther down the tree like creating c in a/b/c
String[] dirSplit = spaceSplit[1].split("/");
int length = dirSplit.length;

FileSystem.mkdir(dirSplit[length-1]);
int directoriesLength = FileSystem.directories.size();

for(int i = 0; i < FileSystem.directories.size(); i++){
if(dirSplit[length-2].equals(FileSystem.directories.get(i))){
FileSystem.directories.get(i).addChild(FileSystem.directories.get(directoriesLength-1));
//Checking if this works
System.out.println("The child was created succesfully");
}
}

} else {
for (int i = 1; i < spaceSplit.length; i++) {
FileSystem.mkdir(spaceSplit[i]);
}
}

} else if (spaceSplit[0].equals("cd")) {
FileSystem.cd(spaceSplit[1]);
} else if (spaceSplit[0].equals("pwd")) {
FileSystem.pwd();
} else if (spaceSplit[0].equals("ls")) {

} else if (spaceSplit[0].equals("edit")) {
String name = spaceSplit[1];
boolean endOfFile = false;
String content = "";

while(endOfFile == false){
String userInput = s.next();
content += userInput;
if(content.contains("<<EOF>>")){
endOfFile = true;
}
}

FileSystem.edit(name, content);


} else if (spaceSplit[0].equals("cat")) {
for(int i = 1; i < spaceSplit.length; i++){
FileSystem.cat(spaceSplit[i]);
}
} else if (spaceSplit[0].equals("updatedb")) {

} else if (spaceSplit[0].equals("locate")) {

} else if (spaceSplit[0].equals("exit")) {
exitProg = true;
System.out.println("Good bye!");
} else {
System.out.println("That is not a command. Please try again.");
}

} while (exitProg == false);

最佳答案

好吧,我想我会在这里回答我自己的问题。现在一切都很完美。

else if (spaceSplit[0].equals("edit")) {
if(spaceSplit.length > 1) {
String name = spaceSplit[1];
boolean endOfFile = false;
String content = "";

while (!(content.contains("<<EOF>>"))) {
String userInput = s.nextLine();
content += userInput + " ";
}

String end = "<<EOF>>";

content = content.replace(end, "");

int size = tree.getTexts().size();

if (size != 0) {
for (int i = 0; i < size; i++) {
if (tree.getTexts().get(i).getName().equals(name)) {
tree.getTexts().get(i).setContent(content);
}
}
tree.edit(name, content);
} else {
tree.edit(name, content);
}
}

}

关于java - 接受用户输入,直到输入 <<EOF>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167042/

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