验证用户如何输入整数?

时间:2019-04-03 15:46:13

标签: java validation integer

我正在尝试验证选择和数量。我只为用户创建5个选择,我不希望他们输入任何String / Char类型或大于5的任何数字。

System.out.print("Please enter the assigned number of the item sold: "); 
    choice = display.nextInt();
switch(choice){ 
            case 1:
                price=one; 
                break;
            case 2:
                price=two;
                break; 
            case 3:
                price=three;
                break; 
            case 4:
                price=four;
                break; 
            case 5:
                price=five;
                break;

    } // end of switch case

对于数量,没有限制,但我不希望用户输入String / Char类型。

System.out.print("Please enter the quantity of the item sold: "); 
    quantity = display.nextInt();

怎么办?

3 个答案:

答案 0 :(得分:0)

向您的交换机添加boolean keepAsking = true; while(keepAsking) { System.out.print("Please enter the assigned number of the item sold: "); int choice = display.nextInt(); switch(choice){ case 1: // stuff keepAsking = false; break; case 2: // stuff keepAsking = false; break; case 3: // stuff keepAsking = false; break; case 4: // stuff keepAsking = false; break; case 5: // stuff keepAsking = false; break; default: System.out.println("Invalid input. Please use 1-5") keepAsking = true; } } 大小写并添加循环机制

 npm install -g create-react-app

答案 1 :(得分:0)

您可以使用Integer库的parseInt函数并将其包装在try / catch中,以验证值是否为数字。

try {
int x = Integer.parseInt(choice);
 // put your switch statement here and it will only be executed if choice can be parsed as an int
} catch (NumberFormatException e) {
// put error-handling or another action here for when it's not an int
}

答案 2 :(得分:0)

使用do { ... }while(condition);循环至少获取一次用户输入,并在条件为false时保持循环。请参见下面的示例。我添加了一些在您的旧/其他问题中找到的代码:

    float one = 244.50f, two = 125.75f, three = 323.33f, four = 46.29f, five = 3323.65f, price; 
    int choice = 0, quantity = 0, yn;
    Scanner display = new Scanner(System.in);
    do{
        System.out.println("Please enter the assigned number of the item sold: "); 
        String ch = display.next();
        try{
            choice = Integer.parseInt(ch);
            if(choice > 5){
                System.out.println("Invalid input. Only numbers 1 - 5 are valid!");  
            }
        } catch(NumberFormatException e){
            System.out.println("Invalid input. Only numbers 1 - 5 are valid!");                 
        }
    }while(choice == 0 || choice > 5);

    switch(choice){ 
        case 1:
            price=one; 
            break;
        case 2:
            price=two;
            break; 
        case 3:
            price=three;
            break; 
        case 4:
            price=four;
            break; 
        case 5:
            price=five;
            break;
    }

    do{
        System.out.println("Please enter the quantity of the item sold: "); 
        String ch = display.next();
        try{
            quantity = Integer.parseInt(ch);
            if(quantity < 1){
                System.out.println("Invalid input. quantity should be >= 1");  
            }
        } catch(NumberFormatException e){
            System.out.println("Invalid input. Only numbers allowed");                 
        }
    }while(quantity < 1);
}