2013-02-18 80 views
0

首先,我不要求任何人做任何事情,只需要一點幫助,以布爾值修復這個錯誤。我把錯誤,但程序停止。我得到了兩個部分的程序。在那裏我做了計算布爾錯誤(FibonacciNumbers)

第一部分:

class FibonacciNumbers { 

    FibonacciNumbers() {} //default constructor 

    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); 
     } 
    } 
} 

二那裏的主要方法是:

import java.util.*; 
public class FibonacciNumbersTesters { 

    public static void main(String[] args) { 
     FibonacciNumbers fNumbers = new FibonacciNumbers(); //creates new object 
     Scanner in = new Scanner(System.in); 
     String again; 
     String test; 
     boolean IsRepeat = true; 
     boolean isQuit; 

     try { 
      isQuit = false; 
      while (!isQuit) { 

       System.out.print("Enter the number you want to convert to Fibanocci('q' to quit): "); 
       int 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? (Y or N): "); 
       again = in.next(); 
       if (again.equalsIgnoreCase("N")) { 

        System.out.println("Thank you! Please terminate the program by entering 'Q' or 'q' OR you can cotinue by entering anything else: "); 
        String toQuit = in.next(); 

        if ((toQuit.charAt(0) == 'q') || (toQuit.charAt(0) == 'Q')) { 
         System.out.println("Good-bye!"); 
         isQuit = true; 

        } 
       } else { 
        IsRepeat = true; 
       } 
      } 
     } catch (InputMismatchException ex) { 

      test = in.nextLine(); 
      if ((test.charAt(0) == 'q') || (test.charAt(0) == 'Q')) { 
       System.out.println("Good-bye!"); 
       isQuit = true; 

      } else { 
       System.out.println("Invalid input!"); 
       System.out.println("Try again! "); 
       isQuit = false; 
      } 
     } 
    } 
} 

這部分我放哪兒isQuit = false;它只是停止結束。我希望它繼續下去。

+1

請編輯問題,以便您的代碼格式正確 - 無論是縮進還是刪除大量無意義的空白行。 – 2013-02-18 20:00:27

回答

1

試着把你的try catch語句放在while循環中。