函数陷入循环
Function stuck in a loop
对于一项学校作业,我的任务是将每项工作分成一个单独的功能。我在使用 doAgain()
时遇到问题的函数是我会选择包含在 main()
中的东西。
我很难让这个功能按我的需要工作。目标是获取用户输入,对其执行操作,然后提示用户查看他们是否想再次 运行 该作业。
当函数 doAgain()
触发时,如果输入“0”,它会终止程序,但如果输入“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
}
非常感谢!
当用户要求再次执行时,您需要将numbersDiffer
的值重新设置为false
,否则将跳过内部while循环并通过调用doAgain
方法(永远...)。
像这样:
while (doItAgain != 0) {
numbersDiffer = false;
while (numbersDiffer != true) {
[...]
或者因为doItAgain
变量是静态的,直接在doAgain
方法中。顺便说一下,boolean
类型会更合适。
您需要将 numbersDiffer
的值重置为 false
在请求输入之后或在内部循环之外添加此行 numbersDiffer = false
。
像这样
doItAgain = (doAgain());
numbersDiffer = false;
你的程序没有执行你的主要逻辑是因为你没有重新设置numbersDiffer
的值,它一直是true
,所以你需要重新设置为false
为了满足条件
对于一项学校作业,我的任务是将每项工作分成一个单独的功能。我在使用 doAgain()
时遇到问题的函数是我会选择包含在 main()
中的东西。
我很难让这个功能按我的需要工作。目标是获取用户输入,对其执行操作,然后提示用户查看他们是否想再次 运行 该作业。
当函数 doAgain()
触发时,如果输入“0”,它会终止程序,但如果输入“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
}
非常感谢!
当用户要求再次执行时,您需要将numbersDiffer
的值重新设置为false
,否则将跳过内部while循环并通过调用doAgain
方法(永远...)。
像这样:
while (doItAgain != 0) {
numbersDiffer = false;
while (numbersDiffer != true) {
[...]
或者因为doItAgain
变量是静态的,直接在doAgain
方法中。顺便说一下,boolean
类型会更合适。
您需要将 numbersDiffer
的值重置为 false
在请求输入之后或在内部循环之外添加此行 numbersDiffer = false
。
像这样
doItAgain = (doAgain());
numbersDiffer = false;
你的程序没有执行你的主要逻辑是因为你没有重新设置numbersDiffer
的值,它一直是true
,所以你需要重新设置为false
为了满足条件