2015-04-07 97 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