2015-04-07 86 views
1

對於一個學校任務,我負責將每項工作分解爲一個單獨的功能。 doAgain()的功能我遇到了麻煩,因爲我只在main()中包含這個選項。功能卡在循環中

我很難讓功能按需要工作。目標是接受用戶輸入,對其執行操作,然後提示用戶看看他們是否想要再次運行該作業。

當函數doAgain()觸發時,如果輸入'0',程序將終止程序,但如果輸入'1',則無法重新運行主程序邏輯。

我確定我錯過了一些簡單的東西,但我一直在b my我的頭。任何機會的人可以提供一些提示?

這裏是我的代碼:

import java.util.Scanner; 

public class numbersAssignment { 

    static int numberOne = 0; 
    static int numberTwo = 0; 
    static int numberThree = 0; 
    static int largest = 0; 
    static int smallest = 0; 
    static int product1 = 0; 
    static int sum = 0; 
    static int average = 0; 
    static boolean numbersDiffer = false; 
    static int doItAgain = 1; 

    public static void main(String[] args) { 

     while (doItAgain != 0) { 
      while (numbersDiffer != true) { 
       numberOne = getNumber(); 
       numberTwo = getNumber(); 
       numberThree = getNumber(); 
       if (verifyDiff(numberOne, numberTwo, numberThree)) { 
        calcPrintNumbers(numberOne, numberTwo, numberThree); 
        numbersDiffer = true; 
       } 
      } 
      //where it all goes wrong - doAgain() stuck... 
      doItAgain = (doAgain()); 
     } 
    }//main 

    /* 
    ****************************************************************************** 
    * getNumber:                 * 
    * This method will ask the user for the number that is to be used in the * 
    * program. All numbers MUST BE INTEGERS, and must use DIFFERENT values. *         * 
    ******************************************************************************/  
    public static int getNumber() { 
       int number = 0; 

       Scanner input = new Scanner(System.in); 
       boolean done = false; 
       while (done != true) { 
        try { 
         System.out.println("Please enter a UNIQUE integer for the program ===> "); 
         number = input.nextInt(); 
         if (number <= 0){ 
          throw new NumberFormatException(); 
         } 
         done = true; 
        }//try 
        catch (Exception message) { 
         input.nextLine(); 
         System.out.println("Bad input, retry"); 
        }//catch 

       }//while 
       return number; 

     }//getNumber 

    /* 
    ****************************************************************************** 
    * calcPrintNumbers:               * 
    *  This method will recieve the three user input variables. The program * 
    *  will then calculate and print, the SUM,AVERAGE,PRODUCT,LARGEST, as well* 
    *  as the SMALLEST of the three numbers. It will then print the results, * 
    *  AS WELL AS THE VALUES STORED IN THE THREE VARIABLES.     *   
    ******************************************************************************/   
    public static void calcPrintNumbers(int numberOne, int numberTwo, int numberThree) 
     { 
       System.out.println("The smallest number is: " + Math.min(numberOne, Math.min(numberTwo, numberThree))); 
       System.out.println("The largest number is: " + Math.max(numberOne, Math.max(numberTwo, numberThree))); 
       System.out.println("The average is: " + ((numberOne + numberTwo + numberThree) /3)); 
       System.out.println("The product is: " + Math.multiplyExact(numberOne, Math.multiplyExact(numberTwo, numberThree))); 
       System.out.println("The sum is: " + (numberOne + numberTwo + numberThree)); 

     }//End of the calcSumPrint method   

    /* 
    ******************************************************************************* 
    * doAgain:                 * 
    * This method will NOT receive incoming data, but it will it will   * 
    * ask for, verify, and return the users choice of whether to continue the * 
    * program or not. The code looks for a choice of 1 to end the program,  * 
    * ANY OTHER INTEGER will continue to run the program.      * 
     ******************************************************************************/ 
    public static int doAgain() 
     { 
       int usersChoice = 0; 
       System.out.println("Enter '0' to quit, or '1' to run again: "); 
       Scanner input = new Scanner(System.in);  
       usersChoice = input.nextInt(); 
       return usersChoice; 
     }//End of the getChoice method   

    /* 
    ******************************************************************************* 
    * verifyDiff:                 * 
    * This method accepts the three variable as arguments. It then compares all*      * 
    * three to see if any values are the same. If they ARE, the method returns * 
    * a false, if all three variable are NOT the same, the method returns true.* 
    *******************************************************************************/ 
    public static boolean verifyDiff(int numberOne, int numberTwo, int numberThree) 
     { 
      boolean allDiff = false;    
      if(numberOne != numberTwo && numberOne != numberThree && numberTwo != numberThree) 
       allDiff = true;  
      else { 
       System.out.println("You tried to use a duplicate number, try again: "); 
      } 
      return allDiff; 
     }//End of the getChoice method   
} 

非常感謝!

回答

2

你需要的numbersDiffer值重置爲false

加入這一行numbersDiffer = false你要求輸入後,或你的內環以外。

這樣

doItAgain = (doAgain()); 
numbersDiffer = false; 

程序爲何沒執行你的主要邏輯的原因是因爲你沒有重置的numbersDiffer的值總是true,所以你需要它重置爲false爲了符合條件

+0

D'oh!非常感謝! – user132791

2

當用戶要求做一遍,你需要的numbersDiffer值重置爲false,或內循環的同時將跳過,執行會通過調用doAgain方法(永遠...)繼續。

像這樣:

while (doItAgain != 0) { 
     numbersDiffer = false; 
     while (numbersDiffer != true) { 
     [...] 

,或者因爲doItAgain變量是靜態的,在直接doAgain方法。順便說一句boolean類型會更適合。

演示:http://ideone.com/HGGbkU