我正在寻找一种方法,基本上可以向用户返回一些看起来与他们键入的内容相同的控制台输出,然后再次提示更多输入。问题是我有一个方法可以修改发现的每个不包含空格的字符串。
在用户给出的每个句子的末尾,我试图找出一种方法来出现换行符,然后提示再次输出到控制台,说“请输入一个句子:”。这是我到目前为止所拥有的...
System.out.print("Please type in a sentence: ");
while(in.hasNext()) {
strInput = in.next();
System.out.print(scramble(strInput) + " ");
if(strInput.equals("q")) {
System.out.println("Exiting program... ");
break;
}
}
以下是控制台输出显示的内容:
Please type in a sentence: Hello this is a test
Hello tihs is a tset
并且光标停在与上例中的“tset”相同的行上。
我想要发生的是:
Please type in a sentence: Hello this is a test
Hello tihs is a tset
Please type in a sentence:
光标出现在“sentence:”之后的同一行
希望这有助于解决我的问题。
最佳答案
试试这个,我没有测试它,但它应该可以满足你的要求。代码中的注释解释了我添加的每一行:
while (true) {
System.out.print("Please type in a sentence: ");
String input = in.nextLine(); //get the input
if (input.equals("quit")) {
System.out.println("Exiting program... ");
break;
}
String[] inputLineWords = input.split(" "); //split the string into an array of words
for (String word : inputLineWords) { //for each word
System.out.print(scramble(word) + " "); //print the scramble followed by a space
}
System.out.println(); //after printing the whole line, go to a new line
}
关于java - 基本控制台输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859959/