[英]How do I fix this infinite while loop
我正在使这个程序能够计算阶乘。 如果他们输入的值超过 20,我需要它说一句话,但我已经创建了无限循环。 我的台词说
System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}
是一个无限循环。 我似乎无法弄清楚如何解决它。 我试过移动东西的位置,但我无法弄清楚。 任何帮助表示赞赏。
import java.util.Scanner;
import java.text.NumberFormat;
public class FactoralApp
{
public static void main(String[] arge)
{
//Welcome users the the application and get user input
System.out.print("Welcome to the Factoral Calculator" + "\n");
int num;
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) { //set up the while loop so that users can find many factorials
long factorial=1; //initialize variables
System.out.print("\n" + "Enter an integer between 1 and 20: "); //promt users for input
num = sc.nextInt();
for (int i = num; i >= 1; i--){ //Calculate factorial
factorial = factorial * i;}
while (num > 20) {
System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}
// Format and display the results
NumberFormat number = NumberFormat.getNumberInstance();
String message =
("\n" + "The factoral of " + num + " is " + number.format(factorial) + "." + "\n"); //create message
System.out.println(message); // Output the formated message
System.out.print ("Continue (y/n): "); //promt users for input
choice = sc.next();
} // End While
} // End main ()
} // End Class
我认为不是 while,您需要一个 if 条件来检查输入的数字是否大于 20。
if(num > 20) { //the result cannot be stored in a long data type so alert
System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}
// Format and display the results
NumberFormat number = NumberFormat.getNumberInstance();
String message =
("\n" + "The factorial of " + num + " is " + number.format(factorial) + "." + "\n"); //create message
System.out.println(message); // Output the formatted message
System.out.print ("Continue (y/n): "); //prompt users for input
choice = sc.next();
} // End if
我建议您了解控制语句的工作原理。 while
是一个循环,显然它会一直运行直到条件满足。 下面是正确的代码。
public static void main(String[] args) { //Welcome users the the application and get user input
System.out.print("Welcome to the Factoral Calculator" + "\n");
int num;
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) { //set up the while loop so that users can find many factorials
long factorial=1; //initialize variables
System.out.print("\n" + "Enter an integer between 1 and 20: "); //promt users for input
num = sc.nextInt();
for (int i = num; i >= 1; i--){ //Calculate factorial
factorial = factorial * i;}
if (num > 20) {
System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}
// Format and display the results
NumberFormat number = NumberFormat.getNumberInstance();
String message =
("\n" + "The factoral of " + num + " is " + number.format(factorial) + "." + "\n"); //create message
System.out.println(message); // Output the formated message
System.out.print ("Continue (y/n): "); //promt users for input
choice = sc.next();
} // End While
}
说明-:仅更新了以下部分代码。 现在上面的代码将打印ERROR: The result is inaccurate because the number was too large for the data type
error 只有一次。
if (num > 20) {
System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}
如果此代码更改不符合您的要求,请提供更多详细信息。 您想打印该错误消息多少次。
您永远不会在 while 循环中更改num
变量的值。 由于这是您在 while 循环的布尔条件中检查的变量,因此条件永远不会改变,并且 while 循环有永远运行的风险。
解决方案:更新循环内的变量。 使用您的 Scanner 变量 sc,就像您在循环之前所做的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.