루프를 사용하여 계승을 계산하는 프로그램의 일부를 작성하려고합니다. 오류 메시지가 없지만 출력이 없습니다.Java에서 루프를 사용하여 계승 계산하는 방법
내 접근 방식에 대한 제안 사항이 있습니까? 아니면 루프를 사용하는 것이 더 나은 방법입니까? 감사!
while (number >= 1);
당신이 세미콜론을 통지하지 않았나요 : 당신의 코드에서
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