功能陷入循环

时间:2015-04-07 00:57:01

标签: java loops while-loop java.util.scanner

对于学校作业,我的任务是将每项工作分成单独的功能。我在doAgain()遇到问题的功能是我在main()中选择的选项。

我很难按照我的需要使功能正常工作。目标是获取用户输入,对其执行操作,然后提示用户查看是否要再次运行作业。

当函数doAgain()触发时,它会终止程序,如果' 0'输入,但如果' 1' 1,则无法重新运行主程序逻辑。进入。

我确定我错过了一些简单的东西,但我一直在敲我的头。任何人都可以提供一些提示吗?

这是我的代码:

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 个答案:

答案 0 :(得分:2)

当用户要求再次执行此操作时,您需要将numbersDiffer的值重置为false,否则将跳过内部while循环并通过调用{{1}继续执行方法(永远......)。

像这样:

doAgain

或因为 while (doItAgain != 0) { numbersDiffer = false; while (numbersDiffer != true) { [...] 变量是静态的,直接在doItAgain方法中。顺便说一下,doAgain类型更适合。

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

答案 1 :(得分:2)

您需要将numbersDiffer的值重置为false

在您要求输入后或在内循环之外添加此行numbersDiffer = false

像这样

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

您的程序未执行主逻辑的原因是您没有重置始终为numbersDiffer的{​​{1}}的值,因此您需要将其重置为true }以满足条件