使用循环进行输入验证和异常处理

时间:2014-07-30 16:14:54

标签: java loops try-catch

如何使用循环进行输入验证以及try和catch块来捕获输入不匹配异常?

这是我的主要方法

import java.util.InputMismatchException;
import java.util.Scanner;

public class labBookFortyThree {
    public static void main(String[] args) {
        Scanner myInput = new Scanner(System.in);
        double circle, rect1, rect2, cyl1, cyl2;
        int input = 0;
        System.out.println("Press 1 to calc circle, 2 for rect, 3 for cyln");

        try {
            input = myInput.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Please enter a proper value");
        }

        if (input == 1) {
            System.out.println("enter radius");
            circle = myInput.nextDouble();
            System.out.println("area of circle is " + areas.area(circle));
        } else if (input == 2) {
            System.out.println("enter length and height");
            rect1 = myInput.nextDouble();
            rect2 = myInput.nextDouble();
            System.out.println("area of rect is " + areas.area(rect1, rect2));
        } else if (input == 3) {
            System.out.println("enter rad, height");
            cyl1 = myInput.nextDouble();
            cyl2 = myInput.nextDouble();
            int useless = 0;
            System.out.println("Area of cyl is " + areas.area(cyl1, cyl2, useless));
        }
    }
}

这是我的班级区域

public class areas {
    public static double area(double rad) {
        double area = 3.14 * (rad * rad);
        return area;
    }

    public static double area(double width, double height) {
        double area = width * height;
        return area;
    }

    public static double area(double rad, double height, int useless) {
        double area = (3.14 * (rad * rad) * height);
        return area;
    }
}

我应该在哪里放置一个输入验证循环?有没有更好的办法?我能在创造性地捕捉或尝试阻止什么?

3 个答案:

答案 0 :(得分:2)

而不是

try {
  input = myInput.nextInt();
} catch (InputMismatchException e) {
  System.out.println("Please enter a proper value");
}

一种可能的方法是使用循环并检查您希望在1到3之间的int输入,

int input = 0;
while(input < 1 || input > 3) {
  if (myInput.hasNextInt()) {
    input = myInput.nextInt();
  } else if (myInput.hasNext()) {
    System.out.println("Please enter a proper value");
    myInput.next();
  } else {
    System.err.println("No more input");
    System.exit(1);
  }
}
// input is 1,2 or 3. Or the program ended.

答案 1 :(得分:0)

您可以使用while循环。

// While the user keeps entering valid input
while(input >= 1 && <= 3)
{
    System.out.println("Press 1 to calc circle, 2 for rect, 3 for cyln");

    try{

        input = myInput.nextInt();
    }

    catch(InputMismatchException e){

        System.out.println("Please enter a proper value");
    }

    if(input == 1)
    {
        System.out.println("enter radius");
        circle = myInput.nextDouble();

        System.out.println("area of circle is " +
        areas.area(circle));

    }

    else if(input == 2)
    {

        System.out.println("enter length and height");
        rect1 = myInput.nextDouble();
        rect2 = myInput.nextDouble();

        System.out.println("area of rect is " + areas.area(rect1,rect2));

    }

    else if(input == 3)
    {
        System.out.println("enter rad, height");
        cyl1 = myInput.nextDouble();
        cyl2 = myInput.nextDouble();

        // int useless = 0;
        // No need to make an integer for it. Just put a 0 there.

        System.out.println("Area of cyl is " + areas.area(cyl1, cyl2, 0));

    }
} // End of while loop

如果您已经了解了这一点,我还想考虑使用switch-case。这里非常有用(更快更容易理解)

// Keep looping forever. If you want it to just keep running when the values are 1, 2, or 3 use:
// while(input >=1 && input <=3) instead of while(true)
while(true)
{
    System.out.println("Press 1 to calc circle, 2 for rect, 3 for cyln");

    try{

        input = myInput.nextInt();
    }

    catch(InputMismatchException e){

        System.out.println("Please enter a proper value");
    }

    switch(input) {
        case 1:
            // When it's 1
            System.out.println("enter radius");
            circle = myInput.nextDouble();

            System.out.println("area of circle is " + areas.area(circle));
            break; // Let's the switch know to stop running code and get out

        case 2:
            // When it's 2
            System.out.println("enter length and height");
            rect1 = myInput.nextDouble();
            rect2 = myInput.nextDouble();

            System.out.println("area of rect is " + areas.area(rect1,rect2));
            break;
        case 3:
            // When it's 3
            System.out.println("enter rad, height");
            cyl1 = myInput.nextDouble();
            cyl2 = myInput.nextDouble();

            // int useless = 0;
            // No need to make an integer for it. Just put a 0 there.

            System.out.println("Area of cyl is " + areas.area(cyl1, cyl2, 0));
            break;
        default:
            System.out.println("You entered an invalid option. Try again");
    }

} // End of while loop

答案 2 :(得分:0)

只是查看代码,我意识到当你要求更多输入时(在堆叠的if语句中)你可能也想要一个类似的try-catch结构。所以我建议创建一个方法来获取输入并检查以确保它是有效的。例如:

public intInp(Scanner s)
{
    boolean v = false;
    int res = 0;
    do
    {
        try
        {
            res = s.nextInt();
            v = true;
        }
        catch(IOException | NumberFormatException e)
        {
            System.out.println("Oops, try again and enter a number please");
        }
    }while(v == false);
    return res;
}

这是双打的:

public double doubleInp(Scanner s)
{
    boolean v = false;
    double res = 0.0
    do
    {
        try
        {
            res = s.nextDouble();
            v = true;
        }
        catch(IOException | NumberFormatException e)
        {
            System.out.println("Oops, try again and enter a number please");
        }
    }while(v == false);
    return res; 
}

然后只需调用方法以保存一些代码。