如何修复此无限循环

时间:2017-02-06 04:00:07

标签: java loops for-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

3 个答案:

答案 0 :(得分:1)

我认为不是一会儿,你需要一个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

答案 1 :(得分:0)

我建议您理解控制声明的工作原理。 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错误。

 if (num > 20) {
            System.out.print ("ERROR: The result is inaccurate because the number was too large for the data type");}   

如果此代码更改不符合您的要求,请提供更多详细信息。您希望打印该错误消息的次数。

答案 2 :(得分:-2)

您永远不会更改while循环中num变量的值。由于这是你在while循环的布尔条件中检查的变量,条件永远不会改变,而while循环可能会永远运行。

解决方案:更新>循环中的变量。使用您的扫描仪变量sc,就像在循环之前一样。