对于一个学校任务,我负责将每项工作分解为一个单独的功能。 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
}
非常感谢!
D'oh!非常感谢! – user132791