如何循环询问数字是否为整数?

时间:2016-01-27 05:14:35

标签: java if-statement while-loop int user-input

x = input.nextInt();

while (!input.hasNextInt(x)) {
    System.out.println("Enter a valid int");
    x = input.nextInt();
}

while (x <=0 || x > 3) {
    System.out.println("Choose a correct gear number: ");
    x = input.nextInt();
}

switch(x) {
    case 1:
        System.out.println("You're in Gear 1");
        break;
    case 2:
        System.out.println("Gear 2");
        break;
    case 3:
        System.out.println("Gear3");
} 

x不是int时,我无法弄清楚如何连续循环请求int。我尝试了很多东西。每次我输入字母或字母组合组合时,如Q23k2k等,我都会收到错配错误。我希望能够检查用户是否在我的方法中输入了他不应该输入的内容。

5 个答案:

答案 0 :(得分:1)

如果使用Scanner类的nextInt()方法来读取像23k这样的字符串值的输入,逻辑很简单,它将抛出一个inputmismatch异常,因为它期望一个Integer值,但是你输入一个String值

因此,您需要使用next()方法将输入作为String读取,然后验证它是否为有效的整数值。

PFObject *orders = [PFObject objectWithClassName:@"Orders"]; 
[orders setObject: products objectForKey:@"P_ID"]; 
[order setObject:@(object.quantity) objectForKey:@"Qty"];

答案 1 :(得分:0)

boto

答案 2 :(得分:0)

  

我希望能够检查用户是否正在输入他的内容   不应该在我的方法中。

   int x = 0;
   while(x <=0 || x > 3){
   try {
         System.out.println("Choose a correct gear number: ");

         x = input.nextInt();
        } catch (InputMismatchException ex) {
        System.out.println("Invalid input");
        input.nextLine();//the scanner can freely read the next
        }
    } 

仅接受输入值123。如果您输入的数字不是24k之类的数字,那么此代码将抛出异常java.util.InputMismatchException并再次询问输入。

答案 3 :(得分:0)

    String str = "1 2 3 4 str";
    Scanner input = new Scanner(str);
    while(input.hasNext()){
        if(input.hasNextInt()){
            int x = input.nextInt();
            switch(x) {
                case 1:
                    System.out.println("You're in Gear 1");
                    break;
                case 2:
                    System.out.println("Gear 2");
                    break;
                case 3:
                    System.out.println("Gear3");
                    break;
                default : System.out.println("Number out of range");
            } 
        }
        else {
            System.out.println("Input not a integer::" + input.next());
        }
    }

答案 4 :(得分:0)

工作解决方案:

import java.util.Scanner;

公共课AskGear {

public static void main(String[] args) {
    int choose = 0;
    Scanner sc = new Scanner(System.in);

    for (;;) {
        System.out.println("Enter your choice: ");
        if (!sc.hasNextInt()) {
            System.out.println("only integers!: ");
            sc.next(); // discard
            continue;
        }

        choose = sc.nextInt();

        if (choose <= 0 || choose > 3) {
            System.out.println("Choose a correct gear number");
            continue;
        }

        boolean choiceMade = false;
        switch (choose) {
        case 1:
            System.out.println("You're in Gear 1");
            choiceMade = true;
            break;
        case 2:
            System.out.println("Gear 2");
            choiceMade = true;
            break;
        case 3:
            System.out.println("Gear3");
            choiceMade = true;

        }
        if (choiceMade) {
            break;
        }
    }

}

}

注意:即使用户输入正确的档位,如果您希望它无休止地运行,您也可以对此代码进行评论:

if (choiceMade) {
        break;
    }