尝试捕获循环

时间:2013-02-24 19:33:12

标签: java

我不是要求任何人做我的工作我只需要帮助解决这种不匹配问题。这是我的计划:

import java.util.InputMismatchException;
import java.util.Scanner;
class FibonacciNumbers {

    FibonacciNumbers()          //default constructor
    {
    }

    Scanner in = new Scanner(System.in);

    public int fOf(int n)
    {

        if (n == 0)                        //the base case
        {

            return 0;

        }
        else if (n==1)
        {

            return 1;
        }

        else 
        {

           return fOf(n-1)+fOf(n-2); 
        }
        }

 public static void main(String[] args)
    {
       FibonacciNumbers fNumbers = new FibonacciNumbers();    //creates new object

        Scanner in = new Scanner(System.in);
        String userInput;
         int n = 0;
        boolean IsRepeat = true ;  
        boolean isQuit; 
        boolean checkException = false;

     isQuit = false;  

     while (!isQuit) 
     {


try {

    {

        System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");


        n = in.nextInt();
        System.out.print("The Fibanocci number for "+n+" is: ");
        n = fNumbers.fOf(n);
        System.out.println(n);

        System.out.print("Do you want to run again? Press 'N' for No or anything else to continue: ");  

        userInput = in.next();  

       if(userInput.equalsIgnoreCase("N") )
          {  
              isQuit = true;
              System.out.println("Good-bye!"); 
           }  
        else 

         {  
        IsRepeat = true; 
         }
       }
    }

catch(InputMismatchException ex) {  
          userInput = in.nextLine();  

           if  ((userInput.charAt(0) == 'q') || (userInput.charAt(0) == 'Q') )  
             {  

                  isQuit = true; 
                  System.out.println("Good-bye!");  

             }  

           else {  
                 checkException = true;
                 IsRepeat = true;
                 System.out.println("Invalid entry, Try again!");
              }    
    }

    catch (ArrayIndexOutOfBoundsException a)
         {
            n = in.nextInt();
             if  (n<0 || n>46) 
                        {
                            System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
                            checkException = false;//sets boolean value to false, continues the loop


                        }
                      else
                          {
                            IsRepeat = true;   
                          }
    }
    }
}
}

我做了所有我得到的一切工作,但在这部分它不会像我希望它一样:

catch (ArrayIndexOutOfBoundsException a)
         {
            n = in.nextInt();
             if  (n<0 || n>46) 
                        {
                            System.out.println("Invalid entry! Please enter an integer that is greater than 0 but less than 46 :");
                            checkException = false;//sets boolean value to false, continues the loop


                        }
                      else
                          {
                            IsRepeat = true;   
                          }
    }

当我运行它时,如果用户输入高于46或低于0,那么请他们输入不同的输入,但它只是在进行数学运算。它不会像我写程序那样做。

1 个答案:

答案 0 :(得分:1)

抛出“java.lang.StackOverflowError”而不是“ArrayIndexOutOfBoundsException”。

更好的方法是在

处捕获无效输入
System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): ");
n = in.nextInt();

你可以设置“n = in.nextInt();”进入do-while循环,

像:

do {
    ask for number
} while (check if number is correct);