[英]How to check entered String in a loop with condition?
I want to write a program, which is checks an entered String of the user for a given char limits (10 chars for instance). 我想编写一个程序,该程序检查给定字符限制(例如10个字符)的用户输入字符串。 If entered String is more than 10 chars, then the system should re-prompt again and again the user to enter the valid String (a word of 10 chars) until the user enter a valid string. 如果输入的字符串超过10个字符,则系统应再次提示用户输入有效的字符串(10个字符的单词),直到用户输入有效的字符串为止。
I have already some piece of code for it, but its not works yet, because I've no idea how to move process up to start (to the line 1) for re-prompt the user or there is other simple way? 我已经有一些代码了,但是它还不能用,因为我不知道如何将进程重新启动(到第1行)以重新提示用户,或者还有其他简单的方法吗?
System.out.print("Input: ");
String inputChars = Input.readString();
while (inputChars.length() > 10){
System.out.print("Input: ");
String inputChars = Input.readString(); // here is mistake now
}
System.out.print("Output: ");
System.out.print("The enter has 10 chars");
I just want to check the entered word, if is more than 10 chars, then just skip it and prompt the user again for entering a word which is not more than 10 chars. 我只想检查输入的单词(如果超过10个字符),然后跳过它并再次提示用户输入不超过10个字符的单词。 I'm not yet good in java, so if this question is stupid just explain me how to solve it. 我的Java语言还不太好,所以如果这个问题很愚蠢,请向我解释如何解决。 Thanks in advance 提前致谢
Look at your loop: 看一下你的循环:
while (inputChars.length() > 10){
System.out.print("Input: ");
String inputChars = Input.readString();
}
The second line of the loop body redeclares the inputChars
variable. 循环主体的第二行重新声明了inputChars
变量。 You can't do that, as it's already in scope. 您不能这样做,因为它已经在范围内。 You want to just replace the previous value : 您只想替换先前的值 :
inputChars = Input.readString();
You should also consider restructuring your code to avoid the duplication though: 但是,您还应该考虑重组代码,以避免重复:
String inputChars;
do {
System.out.print("Input: ");
inputChars = input.readString();
} while (inputChars.length() > 10);
Note how I've also renamed the Input
variable to input
to follow normal Java naming conventions. 请注意,我还如何将Input
变量重命名为input
以遵循常规Java命名约定。 I'd actually probably change both input
and inputChars
to be more descriptive - in particular, there's no indication of what the input data is meant to mean at the moment. 其实我可能会改变这两种input
和inputChars
更描述性的-尤其是,有没有什么样的输入数据意味着此刻的意思表示。
Just remove the String
in beginning: 只需在开头删除String
:
while (inputChars.length() > 10){
System.out.print("Input: ");
inputChars = Input.readString(); // here is mistake now
}
By having String
in beginning, you are attempting the redefine the same name variable again. 通过以String
开头,您正在尝试再次重新定义相同的名称变量。
Change to: 改成:
String inputChars = ""
do {
System.out.print("Input: ");
inputChars = Input.readString();
} while (inputChars.length() > 10)
System.out.print("Output: ");
System.out.print("The enter has 10 chars");
Before, inputChars
had already been declared before the loop, so you didn't need to redeclare the variable. 之前,在循环之前已经声明了inputChars
,因此您无需重新声明变量。
A do-while loop is a better construct here because it makes your intentions more clear and makes your code more clean. do-while循环在这里是一个更好的构造,因为它可以使您的意图更清晰,并使您的代码更清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.