2014-10-19 2 views
0
public static void main (String[] args) 
{ 
    Scanner sc = new Scanner(System.in); 
    int number; 
    System.out.println("Enter a number to find the factorial of it: ");  
    number= sc.nextInt(); 
    int factor=1; 
    if (number<0 && number>10) 
    { 
     System.out.println("Invalid!! the number has to be between 1 and 10"); 
    } 
    for(int x=1; x<=number; x++) 
    {  
     factor = factor*x;    
     System.out.println("The factorial of "+number+" is = " +factor); 
    } 

} 

내 코드 @TNT를 확인할 수 있습니까? 그건 경우 u는 나를 여기함수 및 프로 시저 사용 적용

+0

예 = 1 * 2 * 3 * 4 * 5 –

+0

내가 할 것 어떻게, 내가 사촌 우리는 함수를 사용하여 계승을 사용할 수 있습니다. –

+1

@Makoto : 알고리즘이 정확합니다. System.out 문은 매 반복마다'number'보다는'x'를 반영해야합니다. – Voicu

답변

1

을하고 싶었는지 말해 것은 가능한 솔루션입니다! 예 5

Scanner sc = new Scanner(System.in); 
int number; 
System.out.println("Enter a number to find the factorial of it: "); 
number= sc.nextInt(); 
int factor = 1; 
// edit the condition so numbers that fall outside the range 0-10 will cause the error 
// message to display 
if (number < 1 || number > 10) 
    System.out.println("Invalid!! the number has to be between 1 and 10"); 
else { 
    for(int x=1; x<=number; x++) 
    { 
     factor = factor*x; 
    } 
    System.out.println("The factorial of "+number+" is = " +factor); 
} 
+0

나는 정말로 나를 도와 주셔서 감사합니다. :), thanks alot –

+0

코드를 입력하지 않고도 질문 할 수있는 방법이 있습니까? –

+0

[http] // [this] (http://stackoverflow.com/help/how-to-ask) stackoverflow.com/help/mcve) 괜찮을 것입니다. – TNT

관련 문제