如何验证用户输入的整数?

huangapple go评论110阅读模式
英文:

How do i validate integer as user input?

问题

while (!read.hasNextInt()) {
    System.out.println("Enter integer only:");
    read.next();
}

int num = 0;
num = read.nextInt();
System.out.print(num);

这段代码的作用是验证用户输入是否为整数。以下是解释:

  1. while (!read.hasNextInt()): 这个循环会一直执行,直到用户输入为整数。read.hasNextInt() 返回一个布尔值,表示下一个输入是否是整数。当输入不是整数时,循环条件为真,代码块内的内容将执行。

  2. System.out.println("Enter integer only:");:如果用户输入不是整数,就会打印提示信息,要求用户只输入整数。

  3. read.next();:这行代码的作用是读取并消耗掉非整数的输入。如果不加这行代码,程序会陷入无限循环。原因是,如果用户输入了非整数,它仍然留在输入缓冲区中,不会被 read.nextInt() 消耗掉,而循环条件仍然为真,导致无限循环。

  4. int num = 0;:这里声明了一个整数变量 num,用来存储用户输入的整数。

  5. num = read.nextInt();:这行代码从输入中读取一个整数并将其赋值给变量 num。如果前面已经使用了 read.next() 来消耗掉非整数的输入,那么这里将读取到的是用户输入的正确整数。

  6. System.out.print(num);:最后,将验证过的整数输出。

这段代码通过循环检测用户输入是否为整数,如果不是则提示用户重新输入,直到输入为整数为止。read.next() 用于消耗掉无效输入,确保循环条件在下一次迭代时能够得到正确判断。当输入为整数时,程序将读取并输出该整数。

关于使用 trycatch 的替代方法,你可以使用异常处理来达到类似的效果。不过,根据你的描述,你并不需要使用异常处理来验证输入,因为你已经有了一个工作的解决方案。

英文:

I am required to write a program that will validate the user input as integer. I have a code that will actaully work. But i dont really understand how it works.. Is like i know how to use, but have no idea how it works behind the program. Can you please explain to me how it actually work?

Also, some other alternatives may be using try catch here but i am not required too.. so can anyone please explain to me? i am still new to Java.

Really appreciate that !

while(!read.hasNextInt())  // i understood this pard at which the conditions will return true/false
	{
	    System.out.println("Enter integer only: ");
	    read.next();  // without this line of code, i will get an infinite loop, BUT WHY?
	}
	
	int num = 0;  // declaration of variable
	num = read.nextInt();  // and this actually store the last digit user input in read.hasNextInt()
                           // why would'nt it prompt the user to enter again? because usually it does
	System.out.print(num); // and finally output the num value

答案1

得分: 1

你的 while 循环检查扫描器是否具有可解析的整数,如果条件为真,你只是调用了 next() 方法,它会清除扫描器的缓存。如果你不清除缓存,它将始终具有一个整数,并会持续无限期地阻塞。你需要调用像 next() 或 nextInt() 这样的方法来消耗这个值。

你调用的 nextInt() 并没有再次请求输入,因为你在 while 循环中没有消耗任何内容,你只是检查输入是否为可解析的整数。

以下是你代码的分解(伪代码):

当扫描器没有可解析的整数时 {
    消耗那个不可解析的值
}
消耗可解析的整数
英文:

Your while loop checks if the Scanner doesn't have a parsable Integer by blocking. If that condition is true, you are simply calling next() which clears the Scanners cache. If you didn't clear the cache it would always have an integer and would continue to block indefinitely. You need to call a method like next() or nextInt() to consume the value.

The call to nextInt() that you have doesn't request input again because you haven't consumed anything in your while loop, you just checked if the input was a parsable Integer.

This is the breakdown of your code (pseudo);

while scanner doesnt have a parseable integer {
    consume that non parseable value
}
consume the parseable integer

huangapple
  • 本文由 发表于 2020年4月9日 21:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61122116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定