gpt4 book ai didi

java - 应用函数和过程的使用

转载 作者:行者123 更新时间:2023-12-01 12:24:45 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 应用函数和过程的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26452635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com