简体   繁体   English

如何在有条件的循环中检查输入的字符串?

[英]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. 其实我可能会改变这两种inputinputChars更描述性的-尤其是,有没有什么样的输入数据意味着此刻的意思表示。

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.

相关问题 如何使特定字符串输入while循环的条件,并在每次输入输入时进行检查 - How to make a particular string input the condition for while loop and it checks everytime an input is entered 如何设置在输入特定字符串输入时结束的循环条件 - How to set up a loop condition that ends when a particular string input is entered 输入时如何检查字符串是否在正确的输入中? - How to check if a string is in the correct input when entered? 检查输入的字符串是否为整数 - To check if an entered string is integer 您如何检查用户输入的是字符串值而不是数字? - How do you check that the user entered a String value instead of a number? java - 如何检查用户是否在java中的字符串中输入了正确的字符? - How to check if a correct character was entered by a user in a string in java? 如何检查用户输入的字符串在Java中是否是某个字母? - How to check if a string entered by a user is a certain letter in Java? 如何检查用户输入的字符串是否包含文件扩展名 - How to check if the string the user has entered contains a file extension 如何检查输入的字符串是否包含Unicode:0x1a值 - How to check that entered string contains Unicode: 0x1a value 检查构造函数中是否输入了字符串和整数 - check if string and int entered in constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM