Java 嵌套 While 循环和阶乘初学者
Java Nested While Loops and Factorials Beginner
我正在处理 nest while 循环。我从一个初始的 while 循环开始,打印出给定数字的阶乘。代码如下。我现在正在尝试添加第二个循环,它将包含 10 个数字(即,如果输入数字 4,它将打印出数字 4 到 14 的阶乘)。我知道循环应该从我已有的开始,但我真的不知道从那里去哪里。我已经插入了我认为是第一个循环的开头。
我是编码新手,所以感谢您提供的所有帮助
现有代码:
import java.util.Scanner;
public class MyClass
{
public static void main(String args[])
{
Scanner myScanner = new Scanner(System.in)
while (count <10)
{
int number = myScanner.nextInt();
int fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
`}
}
-您需要将 number = myScanner.nextInt()
移到循环之外,这样它就不会在每次迭代时都要求新的输入。
-您需要定义count
(int count = 0
;)
-循环直到 count
为 <=
10.
-循环结束时递增count
和number
:
Scanner myScanner = new Scanner(System.in);
int count = 0;
int number = myScanner.nextInt();
while (count <=10) {
int fact = 1;
int i = 1;
while(i<=number) {
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
number++;
count++;
}
输出:(输入为 4)
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 11 is: 39916800
Factorial of 12 is: 479001600
Factorial of 13 is: 1932053504
Factorial of 14 is: 1278945280
但是我们可以优化很多。 5!等于4! * 5. 所以知道这一点我们可以简单地不在循环内重置 i
和 fact
。如果我们输入 4,在第一次迭代后,fact
将等于 4!,而 i
将等于 5。如果不重置它们,在下一次迭代中我们将简单地相乘fact
(4!) 乘以 5。然后 i
将变为 6,并且 while
循环将终止。然后这将继续,直到外部 while
循环终止。
int fact = 1;
int i = 1;
while (count <=10) {
while(i<=number) {
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
number++;
count++;
}
将 4 作为输入,这会将内部 while
循环中的迭代次数从 99 减少到 14。
我正在处理 nest while 循环。我从一个初始的 while 循环开始,打印出给定数字的阶乘。代码如下。我现在正在尝试添加第二个循环,它将包含 10 个数字(即,如果输入数字 4,它将打印出数字 4 到 14 的阶乘)。我知道循环应该从我已有的开始,但我真的不知道从那里去哪里。我已经插入了我认为是第一个循环的开头。 我是编码新手,所以感谢您提供的所有帮助
现有代码:
import java.util.Scanner;
public class MyClass
{
public static void main(String args[])
{
Scanner myScanner = new Scanner(System.in)
while (count <10)
{
int number = myScanner.nextInt();
int fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
`}
}
-您需要将 number = myScanner.nextInt()
移到循环之外,这样它就不会在每次迭代时都要求新的输入。
-您需要定义count
(int count = 0
;)
-循环直到 count
为 <=
10.
-循环结束时递增count
和number
:
Scanner myScanner = new Scanner(System.in);
int count = 0;
int number = myScanner.nextInt();
while (count <=10) {
int fact = 1;
int i = 1;
while(i<=number) {
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
number++;
count++;
}
输出:(输入为 4)
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 11 is: 39916800
Factorial of 12 is: 479001600
Factorial of 13 is: 1932053504
Factorial of 14 is: 1278945280
但是我们可以优化很多。 5!等于4! * 5. 所以知道这一点我们可以简单地不在循环内重置 i
和 fact
。如果我们输入 4,在第一次迭代后,fact
将等于 4!,而 i
将等于 5。如果不重置它们,在下一次迭代中我们将简单地相乘fact
(4!) 乘以 5。然后 i
将变为 6,并且 while
循环将终止。然后这将继续,直到外部 while
循环终止。
int fact = 1;
int i = 1;
while (count <=10) {
while(i<=number) {
fact = fact * i;
i++;
}
System.out.println("Factorial of "+number+" is: "+fact);
number++;
count++;
}
将 4 作为输入,这会将内部 while
循环中的迭代次数从 99 减少到 14。