首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >嵌套的try catch

嵌套的try catch
EN

Stack Overflow用户
提问于 2012-10-18 05:24:02
回答 2查看 4.6K关注 0票数 1

我对java非常陌生,并且我已经嵌套了Try Catch代码,可以打印3次!你能告诉我为什么吗?我从命令行得到3int,必须验证并检查它们是否在范围内,我正在使用try和catch。但是答案会打印3次!

代码语言:javascript
运行
复制
// Global Constants
final static int MIN_NUMBER = 1;
final static int MAX_PRIME = 10000;
final static int MAX_FACTORIAL = 12;
final static int MAX_LEAPYEAR = 4000;

// Global Variable
static int a, b, c;

public static void main(String[] args) {
    // String[] myNumbers= new String [3];

    // int x =Integer.parseInt(args[0]);
    for (int i = 0; i < args.length; i++) {
        // System.out.print(args[i]+" ");
        validateInput(args[0], args[1], args[2]);

    }
}

// Validate User Input
public static boolean validateInput(String value1, String value2, String value3) {
    boolean isValid = false;

    try {
        try {
            try {
                a = Integer.parseInt(value1);
                if (!withinRange(a, MIN_NUMBER, MAX_PRIME)) {
                    System.out.println(
                            "The entered value " + value1
                            + " is out of range [1 TO 10000].");
                }
                isValid = true;         

            } catch (Exception ex) { 
                System.out.println(
                        "The entered value " + value1
                        + " is not a valid integer.  Please try again.");
            }   

            b = Integer.parseInt(value2);
            if (!withinRange(b, MIN_NUMBER, MAX_FACTORIAL)) {
                System.out.println(
                        "The entered value " + value2
                        + " is out of range [1 TO 12].");
            }
            isValid = true; 
        } catch (Exception ex) { 
            System.out.println(
                    "The entered value " + value2
                    + " is not a valid integer.  Please try again.");
        }

        c = Integer.parseInt(value3);
        if (!withinRange(c, MIN_NUMBER, MAX_LEAPYEAR)) {
            System.out.println(
                    "The entered value " + value3
                    + " is out of range [1 TO 4000].");
        }
        isValid = true; 
    } catch (Exception ex) { 
        System.out.println(
                "The entered value " + value3
                + " is not a valid integer.  Please try again.");
    }   

    return isValid;

}

// Check the value within the specified range
private static boolean withinRange(int value, int min, int max) {
    boolean isInRange = true; 

    if (value < min || value > max) {
        isInRange = false; 
    }
    return isInRange;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-18 05:29:44

答案被打印了3次,因为您已经将validateInput方法调用放入了main()中的for循环中,如果有三个参数,它将运行三次。

票数 4
EN

Stack Overflow用户

发布于 2012-10-18 05:31:22

我建议编写validateInput方法来验证单个字符串。为最小和最大允许的int值添加两个参数。

代码语言:javascript
运行
复制
public static boolean validateInput(String value, int minValue, int maxValue) {
    try {
        integer intVal = Integer.parseInt(value);
        if (intVal < minVal || intVal > maxValue) {
            System.out.println("The entered value " + value
                + " is out of range [" + minValue + " TO " + maxValue + "].");

            return false;
        }
    } catch (Exception e) {
        System.out.println("The entered value " + value
            + " is not a valid integer. Please try again");
        return false;
    }
    return true;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12943629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档