[英]How do I make it so the user has continuous input but can stop this with the use of a symbol?
I currently have the following code: 我目前有以下代码:
import java.io.*;
import java.util.*;
public class Reverser
{
public void run() throws IOException
{
String text;
String reverseText = "";
Scanner input = new Scanner(System.in);
System.out.println("Please enter text");
text = input.nextLine();
int length = text.length();
for (int i=length-1; i >= 0; i--)
reverseText = reverseText + text.charAt(i);
System.out.println("Your characters in reverse are: "+reverseText);
}
}
I now want to make it so that once the user types in something, they can then type in something else after without having to re-run the program. 现在,我要这样做,以便用户输入内容后,他们便可以在不必重新运行程序的情况下输入其他内容。 I then want to make it so that if the character '*' is entered, it ends and you can no longer enter anything.
然后,我要这样做,以便如果输入字符'*',则结束,并且您不能再输入任何内容。 Is an if statement required after the for loop?
for循环之后是否需要if语句?
You need to use a loop of some sort, eg a while
loop and test the input each time. 您需要使用某种循环,例如
while
循环,并且每次都要测试输入。
For example: 例如:
while (true) {
System.out.println("Please enter text");
text = input.nextLine();
if ("*".equals(text)) {
break;
}
// ...code to use 'text'...
}
The while (true)
loop will run forever, but the test if ("*".equals(text))
will break the loop (using the break
keyword) if an asterix is entered. while (true)
循环将永远运行,但是if ("*".equals(text))
输入星号,则测试if ("*".equals(text))
是否会中断循环(使用break
关键字)。
Is an if statement required after the for loop?
for循环之后是否需要if语句?
No, I don't think so. 不,我不这么认为。
Here is one of the methods to do this 这是执行此操作的方法之一
String text = "";
String reverseText = "";
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Please enter text");
text = input.nextLine();
if (text.equals("*")) {
break;
}
int length = text.length();
for (int i=length-1; i >= 0; i--)
reverseText = reverseText + text.charAt(i);
System.out.println("Your characters in reverse are: "+reverseText);
reverseText = "";
}
Explanation: The code inside the while (true)
block basically keeps looping and looping until input.nextLine()
returns "*"
. 说明:
while (true)
块中的代码基本上保持循环,直到input.nextLine()
返回"*"
为止。 That is what break
does. 这就是
break
作用。
You can also call trim()
on text
so that the loop will also stop if the user enters the asterisk with some whitespace characters. 您还可以在
text
上调用trim()
,这样,如果用户使用一些空白字符输入星号,循环也将停止。
if (text.trim().equals("*"))
Alternatively, you can place the above condition in the parentheses of the while loop. 或者,可以将以上条件放在while循环的括号中。
String text = "";
String reverseText = "";
Scanner input = new Scanner(System.in);
while (!text.trim().equals("*")) {
System.out.println("Please enter text");
text = input.nextLine();
int length = text.length();
for (int i=length-1; i >= 0; i--)
reverseText = reverseText + text.charAt(i);
System.out.println("Your characters in reverse are: "+reverseText);
reverseText = "";
}
But this will behave a little bit differently than the first one. 但这与第一个行为有些不同。 The first method will not print the reverse of * if you ever entered one because the if is placed before the for loop.
如果您曾经输入,第一种方法将不会显示*的反面,因为if放在for循环之前。 On the other hand, the second method will print the reverse of * because the check is done after each iteration of the while loop, as if an if statement is placed at the end of the loop.
另一方面,第二种方法将打印*的反面,因为检查是在while循环的每次迭代之后完成的,就像if语句放在循环的末尾一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.