简体   繁体   中英

How do I fix this infinite while loop

I'm making this program to be able to calculate factorials. I need it to say a sentence if they input a value over 20 but I have created and infinite loop. My line that says

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

Is an infinite loop. I can't seem to figure out how to fix it. I have tried moving the placement of things but I can't figure it out. Any help is appreciated.

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

I think instead of while, you need an if condition to check if the number entered is greater than 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

I suggest you to please understand working of control statements. while is a loop and obviously it will run until and unless condition satisfied. Below is the correct code.

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



}

Description-: Only below section of code has been updated. Now above code will print ERROR: The result is inaccurate because the number was too large for the data type error only one time.

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

If this code change doesn't meet your requirement then provide more details. How many times you want to print that error message.

You never change the value of the num variable within your while loop. Since this is the variable that you're checking in the while loop's boolean condition, the condition will never change , and the while loop risks running forever.

Solution: update the variable within the loop. Use your Scanner variable, sc, just as you did before the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM