在输入有效之前,如何检查无效输入和循环?

我试图从用户输入中找到列表中的最小数字。 我需要询问用户列表中将包含多少个数字(并且只接受正数而不是字母),然后询问他们列表中的数字是什么(仅接受数字)。 如何检查并保持循环直到数字有效?

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); } } } 

让我们为您提供一个样本 ,以便您可以按照您的蓝图进行操作

首先,我选择do while循环,因为你需要至少问一次这个问题。

do … while循环的语法是:

 do { //Statements }while(Boolean_expression); 

请注意,布尔表达式出现在循环的末尾,因此循环中的语句在测试布尔值之前执行一次。

如果布尔表达式为真,则控制流跳回来执行,循环中的语句再次执行。 重复此过程,直到布尔表达式为false。

接下来,你需要看看当输入是正确的时候你如何能够满足boolean_experssion,所以你可以停止循环,或者如果它是错误的,你就会不断提出问题。

我真正喜欢的方式是使用sentinel值,因为使用break关键字真的让我害怕。

在编程中,一个用于终止循环的特殊值。 通常选择标记值,以便不是循环将遇到并尝试执行的合法数据值。 例如,在计算非负整数的循环算法中,值“-1”可以设置为标记值,​​因为计算永远不会将该值作为合法处理输出。

因此,当输入正确时,您可以更改i的值,这样您就可以停止循环或其他方式,显示消息并反复询问问题,直到使用正确答案为止。

代码

  int i = 0; Scanner input = new Scanner(System.in); while (i == 0) { System.out.println("Enter number zero plz"); int result = input.nextInt(); if(result == 0 ){ System.out.println("I entered right number"); i = 1; } else System.out.println("you entered the wrong number \nplz try again"); } 

输出

在此处输入图像描述

由于这显然是一项家庭作业/学习练习,我不会给你代码。 如果您自己进行实际编码,您将了解更多信息。

一旦你修复了循环嵌套的问题……

这段代码有三个问题:

 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!"); } } 

第一个问题是您正在捕获错误的exception。 阅读javadoc 。

第二个问题是如果nextInt失败(由于解析整数的问题),它会将扫描仪的输入光标放回到调用之前的位置。 当你再次调用它时(在下一个循环迭代中),它将再次尝试读取相同的“坏”数字,并再次,…

您必须告诉扫描仪跳过无效的输入行,以便它可以读取用户的下一次尝试。

第三个问题是你没有检查你刚才读的数字是正数!!

最后提示:考虑使用while (true)和条件break ,而不是while (condition) 。 我认为它提供了更优雅的解决方案。


@Kick Buttowski的解决方案通过在每次循环迭代中创建一个新的Scanner来处理错误的输入跳过。 显然它有效…但我有一些疑问1你可以依靠这总是有效。 IMO更好的解决方案是在整个过程中使用一个Scanner ,并使用nextLine()调用来读取和丢弃字符,包括行尾。


1 – 我主要担心的是当你泄漏一个Scanner ,它可能(在某些实现中)关闭终结器中的底层输入流。 如果确实发生了,那么应用程序将停止接受输入。 当前的实现不会这样做,但是没有明确指定(AFAIK)。

在阻止用户前进方面,你的while循环确实没有为你做任何事情。 你可以点击for循环,因为它在你的循环中。 更改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 //