检查用户输入是否为Int而没有例外?

时间:2014-12-01 12:07:34

标签: java exception math

我正在尝试创建一个简单的程序,从用户输入中获取两个数字并将它们相加。我已经把它弄好了正确使用但是如果用户输入别的东西,比如字符串,我试图阻止崩溃。我怎么能检查输入而没有例外,因为我听说它们是不好的做法?

理想情况下,我希望它只是重复要求输入数字的初始问题。我假设这将在一个循环中?

询问用户firstNumber 如果不是int,重复

询问用户secondNumber 如果不是int,重复

做数学

编辑:到目前为止,我几乎都在那里。我已经使用了一些do-while循环来让它重复这个问题,直到输入是一个整数。但是现在即使输入一个整数,它仍然会重复第一个while-do循环。如果我输入一个东西!=整数,它似乎跳到第二个 - 然后停止。

编辑:新代码

import java.util.Scanner;

public class App {

    public static void main(String[] args) {

       Scanner scan;

    scan = new Scanner(System.in);
    int firstNumber = Integer.MIN_VALUE;
    int secondNumber = Integer.MIN_VALUE;

    do {
        System.out.print("Enter the first number to be added: ");
        if (scan.hasNextInt()) {
            firstNumber = scan.nextInt();
        }
    }
    while(firstNumber != Integer.MIN_VALUE);


    do {
        System.out.print("Enter the second number to be added: ");
        if (scan.hasNextInt()) {
            secondNumber = scan.nextInt();
        }
    }
    while(secondNumber != Integer.MIN_VALUE);

    if ((firstNumber != Integer.MIN_VALUE) && (secondNumber != Integer.MIN_VALUE)) {
        int sum = secondNumber + firstNumber;
        System.out.println("That equals: " + sum);
    }
    scan.close();
    }
}

旧代码

import java.util.Scanner;

public class calculate {
    private static Scanner scan;

    public static void main(String[] args) {

        scan = new Scanner(System.in);

        System.out.print("Enter the first number to be added: ");
        int firstNumber = scan.nextInt();

        System.out.println("Enter the second number to be added: ");
        int secondNumber = scan.nextInt();

        int sum = secondNumber + firstNumber;

        System.out.println("That equals: "+ sum);
    }
}

3 个答案:

答案 0 :(得分:1)

您可以简单地使用:

if(scan.hasNextInt()) {//<-- get in if its a number
  int firstNumber = scan.nextInt();
}

答案 1 :(得分:0)

以下内容应该有效:

    Scanner scan;

    scan = new Scanner(System.in);
    int firstNumber = Integer.MIN_VALUE;
    int secondNumber = Integer.MIN_VALUE;

    while (firstNumber == Integer.MIN_VALUE) {
        System.out.print("Enter the first number to be added: ");
        if (scan.hasNextInt()) {
            firstNumber = scan.nextInt();
        } else {
            scan.next();
        }
    }

    while (secondNumber == Integer.MIN_VALUE) {
        System.out.print("Enter the second number to be added: ");
        if (scan.hasNextInt()) {
            secondNumber = scan.nextInt();
        } else {
            scan.next();
        }
    }

    if ((firstNumber != Integer.MIN_VALUE) && (secondNumber != Integer.MIN_VALUE)) {
        int sum = secondNumber + firstNumber;
        System.out.println("That equals: " + sum);
    }
    scan.close();

答案 2 :(得分:0)

您可以使用&#39;尝试捕获&#39;

   boolean flag=false;

   do
   {
       try
        {
         into firstno=scan.nesting();
         flag=true;
        }
          catch(Exception e)
             { 
             System.out.print("please insert only           number");
              }
      }while(flag!=true);

使用try catch可以捕获异常和消息