在Java中调用以前的方法时尝试/捕获错误

时间:2015-03-20 14:02:30

标签: java try-catch jgrasp

使用jGRASP for Mac,我正在为java程序编写代码,首先要求用户输入一个整数[limitNumber],然后计算1和[limitNumber]之间的奇数,并显示这些数字的总和显示值。在第一个方法运行之后,程序将询问用户是否希望再次使用新整数运行计算。

要继续,系统会提示用户输入1表示“是”,输入0表示“否”。如果输入了这些数字中的任何一个,程序运行正常,但我很难尝试合并另一个专注于1和0的try / catch,并为输入的任何其他值抛出异常消息。

代码如下:我试图找出的try / catch是在doAgain()方法中

 import java.util.Scanner; //PRE-WRITTEN CODE FROM SCANNER CLASS

public class sumOddsPetrantoni {

    public static int limitNumber;

    public static void main(String args[]) // MAIN METHOD USED TO CALL OTHER
                                            // METHODS
    { //
        getLimitNumber(); // USER INPUTS NUMBER TO BE USED AS MAX LIMIT
                            //
        calcSumPrint(); // COMP CALCS ODD #S BETWEEN 1 AND LIMIT NUMBER
                        //
        doAgain(); // ASKS USER IF THEY WANT TO REPEAT THE PROCESS
                    // YES: RETURNS TO getLimitNumber METHOD
                    // NO: TERMINATES PROGRAM
                    //
    } // END OF MAIN METHOD

    public static void getLimitNumber() {

        boolean done = false;

        Scanner input = new Scanner(System.in);

        while (done != true) {
            try {
                System.out.print("Let's add some odd numbers!");
                System.out
                        .print("\nPlease enter the limit number as an integer: ");
                limitNumber = input.nextInt(); // METHOD USED TO INPUT INTEGER
                done = true;
            } // END - TRY SECTION
            catch (Exception message) {
                input.nextLine();
                System.out.println("\nDo you know what an integerer is?");
                System.out.println("The program threw a " + message
                        + " message at me, NOT a real integer.");
                System.out.println("Please try again.\n");

            } // END - CATCH SECTION
        } // END - WHILE LOOP

    } // END - GET LIMIT NUMBER METHOD

    public static void calcSumPrint() { // START - CALC SUM PRINT METOD

        int sum = 0;
        int ctr = 0;
        for (ctr = 1; ctr <= limitNumber; ctr += 2)
            // START - FOR LOOP
            sum = sum + ctr;
        {
            System.out.println("\nThe sum of the odd numbers between 1 and "
                    + limitNumber + " is " + sum + ".");
        } // END - FOR LOOP

    } // END - CALC SUM PRINT METHOD

    public static void doAgain() { // START - DO AGAIN METHOD

        int ans;
        Scanner input = new Scanner(System.in);
        System.out
                .println("That was aewsome! \nWant to give it another shot? \n(1 to Continue, 0 to Quit): ");
        ans = input.nextInt();

        if (ans == 1) {
            System.out.println("\nLet's rock and roll!");
            getLimitNumber();
            calcSumPrint();
            doAgain();
        }
        if (ans == 0) {
            System.out.println("Fine, I see how it is...");
        }

    } // END - DO AGAIN METHOD

}

3 个答案:

答案 0 :(得分:0)

您可以在doAgain()方法中执行以下操作:

throw new IllegalArgumentException(ans + " is not allowed value. Only 0 or 1 is allowed");

答案 1 :(得分:0)

为什么不使用else子句并重新启动方法?

if(ans == 1)
  {
  System.out.println("\nLet's rock and roll!");
  getLimitNumber();
  calcSumPrint();
  doAgain();
  }
else if(ans == 0)
  {
  System.out.println("Fine, I see how it is...");
  } 
else {
  System.out.println("What you talkin bout, Willis");
  doAgain();
 }

答案 2 :(得分:0)

import java.util.InputMismatchException;
import java.util.Scanner; //PRE-WRITTEN CODE FROM SCANNER CLASS

public class sumOddsPetrantoni {

    public static int limitNumber;

    public static void main(String args[]) // MAIN METHOD USED TO CALL OTHER
                                            // METHODS
    { //
        getLimitNumber(); // USER INPUTS NUMBER TO BE USED AS MAX LIMIT
                            //
        calcSumPrint(); // COMP CALCS ODD #S BETWEEN 1 AND LIMIT NUMBER
                        //
        doAgain(); // ASKS USER IF THEY WANT TO REPEAT THE PROCESS
                    // YES: RETURNS TO getLimitNumber METHOD
                    // NO: TERMINATES PROGRAM
                    //
    } // END OF MAIN METHOD

    public static void getLimitNumber() {

        boolean done = false;

        Scanner input = new Scanner(System.in);

        while (done != true) {
            try {
                System.out.print("Let's add some odd numbers!");
                System.out
                        .print("\nPlease enter the limit number as an integer: ");
                limitNumber = input.nextInt(); // METHOD USED TO INPUT INTEGER
                done = true;
            } // END - TRY SECTION
            catch (Exception message) {
                input.nextLine();
                System.out.println("\nDo you know what an integerer is?");
                System.out.println("The program threw a " + message
                        + " message at me, NOT a real integer.");
                System.out.println("Please try again.\n");

            } // END - CATCH SECTION
        } // END - WHILE LOOP

    } // END - GET LIMIT NUMBER METHOD

    public static void calcSumPrint() { // START - CALC SUM PRINT METOD

        int sum = 0;
        int ctr = 0;
        for (ctr = 1; ctr <= limitNumber; ctr += 2)
            // START - FOR LOOP
            sum = sum + ctr;
        {
            System.out.println("\nThe sum of the odd numbers between 1 and "
                    + limitNumber + " is " + sum + ".");
        } // END - FOR LOOP

    } // END - CALC SUM PRINT METHOD

    public static int getInt(){
        Scanner input = new Scanner(System.in);
        int ans;
        try {

            ans = input.nextInt();
            return ans;
         }
        catch(Exception e){
            System.out.println("Try Again");
            ans = getInt();
        }
        return ans;
    }
    public static void doAgain() { // START - DO AGAIN METHOD

        int ans;
        Scanner input = new Scanner(System.in);
        System.out
                .println("That was aewsome! \nWant to give it another shot? \n(1 to Continue, 0 to Quit): ");

             ans = getInt();

        if (ans == 1) {
            System.out.println("\nLet's rock and roll!");
            getLimitNumber();
            calcSumPrint();
            doAgain();
        }
        else if (ans == 0) {
            System.out.println("Fine, I see how it is...");
        }else{
            throw new IllegalArgumentException("Oops! that's not fair input must be 0 or 1");
        }



    } // END - DO AGAIN METHOD

}

使用它来处理异常:)