尝试验证java中的用户输入

时间:2014-10-03 02:15:12

标签: java user-input do-while

对不起,伙计们,第一次在这里发布海报,所以如果有礼仪或我应该关注的任何事情,请告诉我!

我在论坛上看过其中的一些,我已经仔细研究过它们,尝试做一件事,但我似乎无法做到。

我需要创建一个程序,要求用户输入两个正整数。每次回答整数时,我想验证以确保它是1)数字和2)也是正数。如果不是,我应该只是终止程序。到目前为止,我已经尝试过:

import java.util.Scanner;

public class Assignment4 {

    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter two positive integers.");
        System.out.println("Please enter the first integer:");
        int num1 = in.nextInt();
    do {
        System.out.println("Invalid. ***End of Program***");
        System.exit(0);
        while (!in.hasNextInt()) {
            System.out.println("Invalid. ***End of Program***");
            System.exit(0);
                    }
        }while (num1 <= 0);
        }
    }
}

适用于输入负数时,但由于某种原因,java只会收到无效的错误输入,并且在输入字母时不会运行。我也尝试过:

import java.util.Scanner;

public class Assignment4 {

    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter two positive integers.");
        System.out.println("Please enter the first integer:");
        int num1 = in.nextInt()
        if (!in.hasNextInt()){
            System.out.println("Invalid integer. ***End of Program***");
            System.exit(0);
        }else if (num1 <= 0){
            System.out.println("Invalid negative number. ***End of Program***");
            System.exit(0);
}
}
}

它什么也没做。昨天我已经遇到了这个问题大约2个小时,今天还有一个小时,在网上寻找其他解决方案。我不能使用任何匹配或try / catch语句,因为我还没有学到它们!任何见解都会很棒,因为我在这里疯了。

3 个答案:

答案 0 :(得分:1)

使用InputMismatchException捕获整数时使用nextInt()

    int num1 = 0;
    try {
       num1 = in.nextInt()
       if(num1 < 0) {
         System.out.println(num1 + " is not a positive integer");
       }
    } catch(InputMismatchException ime) {
        System.out.println(num1 + " is not a valid integer");
    }

答案 1 :(得分:1)

这里的问题是您尝试以用户输入的int值读取。

如果输入恰好是整数以外的其他内容,程序将返回InputMismatchException

为了解决这个问题,我们将执行以下操作:

int num1, num2 = 0;

try {
    System.out.println("Please enter two positive integers.");
    System.out.println("Please enter the first integer:");
    num1 = in.nextInt();

    if(num1 < 0) {
        System.out.println("Invalid integer. ***End of Program***");
        System.exit(0);

    } else {
        System.out.println("Please enter the second integer:");
        num2 = in.nextInt();

        if(num2 < 0) {
            System.out.println("Invalid integer. ***End of Program***");
            System.exit(0);
        }
} catch(InputMismatchException e) {
    System.out.println("That is not an integer. ***End of Program***");
    System.exit(0);
}
...

希望这有帮助!

答案 2 :(得分:0)

在输入信件时,您会收到错误,因为您使用in.nextInt()来阅读文本。

尝试将代码置于try / catch block-

try{
     int num1 = in.nextInt();
     if (!in.hasNextInt())
     {
         System.out.println("Invalid integer. ***End of Program***");
         System.exit(0);
     }else if (num1 <= 0){
         System.out.println("Invalid negative number. ***End of Program***");
         System.exit(0);
     }
    }
    catch(InputMismatchException ime) {
         System.out.println(" not a valid integer");
    }