簡體   English   中英

Java階乘輸出

[英]Java factorial output

我應該創建一個程序,詢問用戶一個數字,然后取這個數字的階乘然后詢問他們是否想要做另一個階乘(Y,N)。

它應該像這樣工作:

  • 輸入一個數字,取以下因子:4
  • 4! = 24
  • 做另一個因子(Y,N)? ÿ
  • 重復,直到輸入N.

我的輸出如下:

  • 輸入一個數字,取以下因子:
  • “做另一個因子?(Y,N)?”
  • 4! 無論我輸入Y還是N,都= 1。

    這是我的代碼:

     import java.util.Scanner; public class factorial { public static void main ( String [] args ) { Scanner input = new Scanner(System.in); System.out.print("Enter a number you want to take the factorial of: "); int num = input.nextInt(); int fact = 1; System.out.printf("%d! = %d\\n ", num, fact, Factorial(num, fact)); } public static int Factorial(int num, int fact) { Scanner input = new Scanner(System.in); char foo; System.out.print("Do another factorial (Y,N)?"); foo = input.next().charAt(0); for (int i = 1; i >= num; i++) { fact *= i; if (foo == 'Y') { System.out.print("Do another factorial (Y,N)?"); foo = input.next().charAt(0); continue; } else { break; } } return fact; } } 

改變后:

import java.util.Scanner;
public class factorial
{
    public static void main ( String [] args )
    {
      Scanner input = new Scanner(System.in);

        System.out.print("Enter a number you want to take the factorial of: ");
        int num = input.nextInt();

        int fact = 1;

        System.out.printf("%d! = %d\n ", num, Factorial(num, fact));

        System.out.print("Do another factorial (Y,N)? ");
        char  foo = input.next().charAt(0);

        while (foo != 'N')
        {
            System.out.print("Do another factorial (Y,N)? ");
            foo = input.next().charAt(0);

        System.out.print("Enter a number you want to take the factorial of: ");
        num = input.nextInt();

        System.out.printf("%d! = %d\n", num, Factorial(num, fact));
    }
}
    public static int Factorial(int num, int fact)
    {
        for (int i = 1; i <= num; i++)
            {   
                fact *= i;
            }
            return fact; 
    }

}

輸出仍有一些問題:

  • 輸入一個數字,取以下因子:4
  • 4! = 24
  • 做另一個因子(Y,N)? ÿ
  • 做另一個因子(Y,N)? ÿ
  • 輸入一個數字,取以下因子:4
  • 4! = 24
  • 做另一個因子(Y,N)? ñ
  • 輸入一個數字,取以下因子:

你計算階乘,但你永遠不會打印它:

System.out.printf("%d! = %d\n ", num, fact, Factorial(num, fact));

應該

System.out.printf("%d! = %d\n ", num, Factorial(num, fact));

此外,您的Factorial函數不使用fact參數,因此您應該刪除它,並在函數內聲明一個局部變量。

最后,要求“你想要另一個因子”應該在頂級,而不是在Factorial函數內部。 您的代碼也不使用用戶輸入的字符:您需要一個檢查用戶輸入的循環,並在輸入Y繼續。

這是錯誤:

System.out.printf("%d! = %d\n ", num, fact, Factorial(num, fact))

你的輸出不是Factorial(num, fact)fact ,這應該是:

System.out.printf("%d! = %d\n ", num, Factorial(num, fact))

刪除下面的代碼行:

掃描儀輸入=新掃描儀(System.in); System.out.print(“做另一個因子(Y,N)?”);

從因子法。

您應該按如下方式重構代碼:

  • 應該有一個方法接受一個int並返回它的階乘,而不做任何其他事情。

  • 所有的輸入/輸出,以及“做另一個因子?” 循環,應該在main()

這樣可以更容易地獲得正確的邏輯。

這會對你有所幫助。

public static void main ( String [] args )
     {
     String yesno ="Y";
     while(yesno.equalsIgnoreCase("y"))
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter a number you want to take the factorial of: ");
         int num = input.nextInt();
         System.out.printf("%d! = %d\n ",num,fact(num));
         System.out.print("Do another factorial (Y,N)?");
         Scanner inputKey = new Scanner(System.in);
         yesno = inputKey.nextLine();

     }
     }

這是階乘函數:

public static int fact(int num)
    {
        if(num<=1)
        {
            return 1;

        }else{
            return (num*(fact(num-1))); 
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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