簡體   English   中英

我如何解決這個無限的 while 循環

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM