Java 如何检查无效输入并循环,直到输入有效?
我试图从用户输入中找到列表中的最小数字。我需要问用户列表中有多少个数字,只接受正数,不接受字母,然后问他们列表中的数字是什么,只接受数字。我怎样才能检查并保持循环直到数字有效Java 如何检查无效输入并循环,直到输入有效?,java,list,loops,Java,List,Loops,我试图从用户输入中找到列表中的最小数字。我需要问用户列表中有多少个数字,只接受正数,不接受字母,然后问他们列表中的数字是什么,只接受数字。我怎样才能检查并保持循环直到数字有效 public class SmallestInt { /** * @param args the command line arguments */ public static void main(String[] args) { // Initialize a S
public class SmallestInt {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Initialize a Scanner to read input from the command line
Scanner input = new Scanner(System. in );
int totalIntegers = 1;
int num = 0;
int smallest = 0;
boolean inputValid = false;
/* Prompt the user and validate their input to ensure they've entered a positive (greater than zero) integer. Discard/ignore any other data.
*/
while (!inputValid)
{
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
totalIntegers = input.nextInt();
inputValid = true;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
/* Read in the candidates for smallest integer
* Validate this input as well, this time ensuring that the user has provided a valid int (any int will do at this point) and discarding any other data
*/
for (int ii = 1; ii <= totalIntegers; ii++) {
// Prompt
System.out.print("Enter value " + ii + ": ");
num = input.nextInt();
if (ii == 1) smallest = num;
else if (num < smallest) smallest = num;
}
// display smallest int
System.out.println("The smallest number entered was: " + smallest);
}
}
}
您的while循环在阻止用户前进方面对您没有任何帮助。您能够点击for循环,因为它在您的while循环中。更改while循环和for循环,使for循环位于while循环之外
while (!inputValid)
{
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
totalIntegers = input.nextInt();
inputValid = true;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
} // End while //
/* Read in the candidates for smallest integer
* Validate this input as well, this time ensuring that the user has provided a valid int (any int will do at this point) and discarding any other data
*/
for (int ii = 1; ii <= totalIntegers; ii++) {
// Prompt
System.out.print("Enter value " + ii + ": ");
num = input.nextInt();
if (ii == 1) smallest = num;
else if (num < smallest) smallest = num;
} // End for //
您的while循环在阻止用户前进方面对您没有任何帮助。您能够点击for循环,因为它在您的while循环中。更改while循环和for循环,使for循环位于while循环之外
while (!inputValid)
{
System.out.print("How many integers shall we compare? (Enter a positive integer): ");
try {
totalIntegers = input.nextInt();
inputValid = true;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
} // End while //
/* Read in the candidates for smallest integer
* Validate this input as well, this time ensuring that the user has provided a valid int (any int will do at this point) and discarding any other data
*/
for (int ii = 1; ii <= totalIntegers; ii++) {
// Prompt
System.out.print("Enter value " + ii + ": ");
num = input.nextInt();
if (ii == 1) smallest = num;
else if (num < smallest) smallest = num;
} // End for //
因为这显然是一个家庭作业/学习练习,所以我不会给你代码。如果你自己编写代码,你会学到更多 一旦修复了循环嵌套的问题 此代码有三个问题:
while (!inputValid) {
System.out.print("How many integers? (Enter a positive integer): ");
try {
totalIntegers = input.nextInt();
inputValid = true;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
}
第一个问题是您捕获了错误的异常
第二个问题是,如果nextInt由于解析整数的问题而失败,它会将扫描仪的输入光标放回调用之前的位置。当你在下一个循环迭代中再次调用它时,它会一次又一次地尝试读取相同的坏数字
您必须告诉扫描仪跳过无效的输入行,以便它可以读取用户的下一次尝试
第三个问题是你没有检查你刚刚读到的数字是否为正
最后提示:考虑使用TrimeTrand和条件中断,而不考虑条件。我认为它提供了一个更优雅的解决方案
@Kick Buttowski的解决方案通过在每个循环迭代中创建一个新的扫描程序来处理错误的输入跳过。很明显,它是有效的。。。但我有一些疑问,你可以相信这一点,它总是有效的。IMO一个更好的解决方案是在整个过程中使用一个扫描仪,并使用下一行调用来读取和丢弃行末(包括行末)的字符1-我主要担心的是,当您泄漏扫描仪时,在某些实现中可能会关闭终结器中的底层输入流。如果确实发生了这种情况,那么应用程序将停止接受输入。当前的实现没有做到这一点,但这在AIK中没有明确规定。因为这显然是一个家庭作业/学习练习,所以我不会给你代码。如果你自己编写代码,你会学到更多 一旦修复了循环嵌套的问题 此代码有三个问题:
while (!inputValid) {
System.out.print("How many integers? (Enter a positive integer): ");
try {
totalIntegers = input.nextInt();
inputValid = true;
} catch (NumberFormatException e) {
System.out.println("Invalid input!");
}
}
第一个问题是您捕获了错误的异常
第二个问题是,如果nextInt由于解析整数的问题而失败,它会将扫描仪的输入光标放回调用之前的位置。当你在下一个循环迭代中再次调用它时,它会一次又一次地尝试读取相同的坏数字
您必须告诉扫描仪跳过无效的输入行,以便它可以读取用户的下一次尝试
第三个问题是你没有检查你刚刚读到的数字是否为正
最后提示:考虑使用TrimeTrand和条件中断,而不考虑条件。我认为它提供了一个更优雅的解决方案
@Kick Buttowski的解决方案通过在每个循环迭代中创建一个新的扫描程序来处理错误的输入跳过。很明显,它是有效的。。。但我有一些疑问,你可以相信这一点,它总是有效的。IMO一个更好的解决方案是在整个过程中使用一个扫描仪,并使用下一行调用来读取和丢弃行末(包括行末)的字符 1-我主要担心的是,当您泄漏扫描仪时,在某些实现中可能会关闭终结器中的底层输入流。如果确实发生了这种情况,那么应用程序将停止接受输入。当前的实现没有做到这一点,但这一点在AIK中没有明确规定。让我们为您提供一个示例,以便您可以按照您的蓝图进行操作 首先,我选择DoWhile循环,因为您至少需要问一次这个问题 do…while循环的语法为: 请注意,布尔表达式出现在循环的末尾,因此 循环中的语句在测试布尔值之前执行一次 如果布尔表达式为true,则控制流将跳回 ,循环中的语句将再次执行。这个过程 重复,直到布尔表达式为false 接下来,您需要了解如何在输入正确时稳定布尔表达式,以便停止循环,或者如果循环错误,则继续提问 我真正喜欢的方式是使用se 因为使用break关键字真的让我害怕 在程序设计中,用于终止循环的一种特殊值。这个 sentinel值通常被选择为非合法数据 循环将遇到并尝试使用的值。对于 例如,在计算非负整数的循环算法中 值-1可以根据计算结果设置为sentinel值 永远不要将该值作为合法的处理输出 因此,当输入正确时,您可以更改i的值,这样您就可以停止循环或其他操作,显示消息并反复询问问题,直到使用找到正确的答案 代码: 输出: 让我们为您提供一个样本,以便您可以按照您的蓝图进行操作 首先,我选择DoWhile循环,因为您至少需要问一次这个问题 do…while循环的语法为: 请注意,布尔表达式出现在循环的末尾,因此 循环中的语句在测试布尔值之前执行一次 如果布尔表达式为true,则控制流将跳回 ,循环中的语句将再次执行。这个过程 重复,直到布尔表达式为false 接下来,您需要了解如何在输入正确时稳定布尔表达式,以便停止循环,或者如果循环错误,则继续提问 我真正喜欢的方式是使用sentinel值,因为使用break关键字真的让我害怕 在程序设计中,用于终止循环的一种特殊值。这个 sentinel值通常被选择为非合法数据 循环将遇到并尝试使用的值。对于 例如,在计算非负整数的循环算法中 值-1可以根据计算结果设置为sentinel值 永远不要将该值作为合法的处理输出 因此,当输入正确时,您可以更改i的值,这样您就可以停止循环或其他操作,显示消息并反复询问问题,直到使用找到正确的答案 代码: 输出:为什么将}}留在代码块之外的打字错误如此常见?代码的哪一部分让您感到不安?还是你不明白?你能说得更具体一点吗?事实上,这在堆栈溢出之前已经被问过很多次了,快速搜索也不会有什么坏处。您想了解的是do while循环,它可以让您循环某些代码,直到满足特定条件。至于您的问题,我会尝试将实际使用totalinteger的部分放在while循环之外,在while循环中,您试图读取和验证它。为什么将}}放在代码块之外的打字错误如此常见?代码的哪一部分让您感到困扰?还是你不明白?你能说得更具体一点吗?事实上,这在堆栈溢出之前已经被问过很多次了,快速搜索也不会有什么坏处。您想了解的是do while循环,它可以让您循环某些代码,直到满足特定条件。至于您的问题,我会尝试将实际使用totalinteger的部分留在while循环之外,在while循环中,您将尝试读取和验证it@KickButtowski-我不知道有哪种情况会失败。我只是说我个人会以不同的方式实现它;i、 e.使用readLine清除不良输入。这种方法的潜在问题更少,因为它不依赖javadoc没有明确指定的行为。非常感谢您提供以下信息:第二个问题是,如果nextInt由于解析整数的问题而失败,它会将扫描仪的输入光标放回调用前的位置。当你在下一个循环迭代中再次调用它时,它会一次又一次地尝试读取相同的坏数字。。。您必须告诉扫描仪跳过无效的输入行,以便它可以读取用户的下一次尝试。@KickButtowski-我不知道有哪种情况会失败。我只是说我个人会以不同的方式实现它;i、 e.使用readLine清除不良输入。这种方法的潜在问题更少,因为它不依赖javadoc没有明确指定的行为。非常感谢您提供以下信息:第二个问题是,如果nextInt由于解析整数的问题而失败,它会将扫描仪的输入光标放回调用前的位置。当你在下一个循环迭代中再次调用它时,它会一次又一次地尝试读取相同的坏数字。。。您必须告诉扫描仪跳过无效的输入行,以便它可以读取用户的下一次尝试。