如何提示用户输入0到100之间的数字? [英] How can I prompt a user for a number between 0 and 100?

查看:120
本文介绍了如何提示用户输入0到100之间的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将用户输入作为0到100之间的int,并提示用户再试一次,只要他们的输入不符合此条件。

Trying to get user input as an int between 0 and 100, and prompt the user to "try again" as long as their input does not match this criteria.

到目前为止,我的代码在实现我的目标方面有些成功:

My code thus far is somewhat successful at accomplishing my goal:

import java.util.Scanner;

public class FinalTestGrade
{
    public static void main(String[] args)
    {
        Scanner studentInput = new Scanner (System.in );
        int testGrade;

        System.out.println("Please enter your test grade (0 to 100)");
        while (!studentInput.hasNextInt()) 
            {
            System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");
            studentInput.next();
            }
        testGrade = studentInput.nextInt();

        while (testGrade > 100 || testGrade < 0) 
            {
            System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");
            testGrade = studentInput.nextInt();
            }

如您所见,程序将检查输入是否为int。一旦用户成功输入int,程序将检查以确保它们的输入介于0和100之间。当用户在第二个提示符(由第二个while循环启动)中输入非int响应
时出现问题)。以下是一个示例:

As you can see, the program will check that the input is an int. Once the user successfully enters an int, the program checks to make sure that their input is between 0 and 100. The problem arises when the user enters a non int in response to the second prompt (initiated by the second while loop). Below is an example:

run:
Please enter your test grade (0 to 100)
P
Your input does not match the criteria, please enter a number between 0 and 100
109
Your input does not match the criteria, please enter a number between 0 and 100
P
    Exception in thread "main" java.util.InputMismatchException
            at java.util.Scanner.throwFor(Scanner.java:840)
            at java.util.Scanner.next(Scanner.java:1461)
            at java.util.Scanner.nextInt(Scanner.java:2091)
            at java.util.Scanner.nextInt(Scanner.java:2050)
            at finaltestgrade.FinalTestGrade.main(FinalTestGrade.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)

这么长的故事简短,我想知道是否有办法合并我的while循环,以便接受表示为0到100之间的int的输入。保存为变量。所有不符合此标准的输入都应触发一个重复的提示,直到输入符合此标准。有什么建议吗?

So long story short, I'm wondering if there's a way to combine my while loops so that input expressed as an int between 0 and 100 is accepted & saved as a variable. All input which doesn't meet this criteria should trigger a prompt which repeats until the input meets this criteria. Any suggestions?

推荐答案

int testGrade = -1 ;
Scanner studentInput = new Scanner(System.in);
while (testGrade > 100 || testGrade < 0) 
{
   System.out.println("Your input does not match the criteria, please enter a number between 0 and 100");

   while(!studentInput.hasNextInt())
   {
       studentInput.next() ;
   }
   testGrade = studentInput.nextInt();
}

有一个无限循环来检查流中是否有无效字符。如果是这样,请使用它,即 hasNextInt()用于。如果输入有效的内容,则退出该循环。

Have an infinite loop to check if there are invalid characters in the stream. If so, consume it, that is what hasNextInt() is for. If you enter something valid, that loop exits.

这篇关于如何提示用户输入0到100之间的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆