Try-Catch和If Statement的问题

时间:2014-06-12 12:06:25

标签: java if-statement try-catch

我目前正在制作一个应用程序,其中用户输入货币值,并且值的更改是使用尽可能少的硬币生成的。我已经使程序工作但我在验证方面遇到了问题。为了确保只输入货币值,我有一个try catch来确保没有输入字母和一个if语句来确保只输入两位小数。为了尝试使它更整洁我将它们分成两个不同的方法,这些方法在我的主要执行。

这两种方法都可以完成工作。我遇到的问题是,一旦他们产生了消息,即使没有输入适当的货币价值,程序的其余部分仍然会运行。如何创建它以便提示用户输入另一个号码。

所有相关代码如下所示。先感谢您。 这就是主要的样子:

public static void main(String[] args ) {

    GetValue.AskValue();
    GetValue.CheckValue1();
    GetValue.CheckValue2();
    CalculateChange.Rounding();
    CalculateChange.GenerateDollars();
    CalculateChange.GenerateHalfDollar();
    CalculateChange.GenerateQuarter();
    CalculateChange.GenerateDime();
    CalculateChange.GeneratePennyX2();
    CalculateChange.GeneratePenny();
    CalculateChange.GenerateResults();

}

这些位于一个类别中:

static void CheckValue1(){

        Scanner sc = new Scanner(System.in);    
        try {
            System.out.println("Please input an integer:  ");
            //nextInt will throw InputMismatchException
            //if the next token does not match the Integer
            //regular expression, or is out of range
            number =sc.nextDouble();
        } catch(InputMismatchException exception) {
                //Print "This is not an integer"
            //when user put other than integer
            System.out.println("   Please do not type letters");

            AskValue();
       }
       // number = sc.nextDouble();


}
static void CheckValue2(){

        String[] splitter = Double.toString(number).split("\\.");
        splitter[0].length();   // Before Decimal Count
        int decimalLength = splitter[1].length();  // After Decimal Count
        if (decimalLength <= 2){
            java.math.BigDecimal bd = new java.math.BigDecimal(String.valueOf(number));
            Input = Input.add(bd);
        } else{ 
            System.out.println(number +"  Is not a valid number. You may only go to two decimal palces");   
        }

}

3 个答案:

答案 0 :(得分:1)

我认为最佳做法是从方法中抛出异常,如下所示:

static void CheckValue1() throws InputMismatchException {
    ...
    number = sc.nextDouble(); // no try-catch
}

static void CheckValue2() throws InputMismatchException {
    if (decimalLength <= 2) {
        ...
    } else {
        throw new InputMismatchException("...");
    }

public static void main(String[] args ) {
    boolean success;
    while (!success)
        try {
            GetValue.AskValue();
            GetValue.CheckValue1();
            GetValue.CheckValue2();
            success = true;
        } catch (InputMismatchException e) {
            ...
        }
    }
}

这样就可以分离逻辑和错误处理。

答案 1 :(得分:0)

像这样改变:

在Main:

do {
    GetValue.AskValue();
} while (!GetValue.CheckValue1());

在CheckValue1中:

 static boolean CheckValue1(){

 Scanner sc = new Scanner(System.in);    
 try {
          System.out.println("Please input an integer:  ");
          number =sc.nextDouble();
          return true;
 }
 catch(InputMismatchException exception) {
          //Print "This is not an integer"
          //when user put other than integer
            System.out.println("   Please do not type letters");


            return false;
        }
}

答案 2 :(得分:0)

一种可能性:再次询问价值并再次检查。请注意,这可能不是最美丽的方式。我其实更喜欢

static void CheckValue1(){

    Scanner sc = new Scanner(System.in);    
    try
        {
          System.out.println("Please input an integer:  ");
          //nextInt will throw InputMismatchException
          //if the next token does not match the Integer
          //regular expression, or is out of range
          number =sc.nextDouble();
        }
        catch(InputMismatchException exception)
        {
          //Print "This is not an integer"
          //when user put other than integer
            System.out.println("   Please do not type letters");


            AskValue();  /////// Ask for new value & check
            CheckValue1();
        }
   // number = sc.nextDouble();


}
static void CheckValue2(){

    String[] splitter = Double.toString(number).split("\\.");
    splitter[0].length();   // Before Decimal Count
    int decimalLength = splitter[1].length();  // After Decimal Count
    if (decimalLength <= 2){
    java.math.BigDecimal bd = new java.math.BigDecimal(String.valueOf(number));
    Input = Input.add(bd);
    }
    else{   
        System.out.println(number +"  Is not a valid number. You may only go to two decimal palces");   
        AskValue();   ///////////// Ask for new value & check
        CheckValue1();
        CheckValue2();
    }

 }