ループを使用して階乗を計算するプログラムの一部を記述しようとしています。エラーメッセージは表示されませんが、出力が得られません。ループで階乗を計算する方法
私のアプローチについての提案はありますか、ループを使用するより良いアプローチがありますか?ありがとう!あなたのコードで
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.print("Enter a non-negative number that you wish to perform a factorial function on: ");
//Create scanner object for reading user input
Scanner input = new Scanner(System.in);
//Declare variables
int number = input.nextInt();
int factTotal = 1;
//Execute factorial
do{
factTotal = factTotal * number;
number--;
while (number >= 1);
}
while (number <= 0);{
System.out.println("That's not a positive integer!");
}
System.out.print(factTotal);
}
}
*「エラーメッセージは表示されませんが、出力が得られません」* - デバッガを使用してみてください! –
'while(number> = 1);' while(number> = 1){/ * do nothing * /} 'と同じで、永遠に実行されます。 – Andreas
階乗の計算は正の数を掛けることを意味するので、while(number <= 0)は何をすべきでしょうか? 'number'が負である間にループすることは、このコードの中には何もないようです。 – Andreas