应用函数和过程

时间:2014-10-19 16:23:47

标签: java bluej

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吗?告诉我,这是你想要我做的事情

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案:

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);
}