简体   繁体   English

Java嵌套While循环和阶乘初学者

[英]Java Nested While Loops and Factorials Beginner

I'm working on nest while loops. 我正在嵌套while循环。 I began with an initial while loop, that printed out the factorial of a given number. 我从一个初始的while循环开始,该循环打印出给定数字的阶乘。 The code for that is below. 下面的代码。 I am now trying to to add the second loop, which would 10 numbers, (ie if the number 4 was inputted, it would print out the factorial for numbers 4 through to 14). 我现在尝试添加第二个循环,该循环将包含10个数字(即,如果输入数字4,它将打印出从4到14的阶乘)。 I know the loop should begin above what I already have, but I don't really know where to go from there. 我知道循环应该从我已经拥有的循环开始,但是我真的不知道从那里开始。 I've inserted what I think is the beginning of the first loop. 我插入了我认为是第一个循环的开始的内容。 I'm new to coding so any and all help is appreciated 我是编码新手,因此不胜感激

Existing code: 现有代码:

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

-You need to move the number = myScanner.nextInt() outside of the loop so that it won't ask for new input every iteration. -您需要将number = myScanner.nextInt()循环,以免每次迭代都要求新的输入。

-You need to define count ( int count = 0 ;) -您需要定义countint count = 0 ;)

-Loop until count is <= 10. -循环直到count <= 10。

-Increment count and number at the end of the loop: -循环末尾的增量countnumber

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

Output: (With 4 as input) 输出:(输入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

However we can optimize this a lot. 但是,我们可以对此进行很多优化。 5! 5! is equal to 4! 等于4! * 5. So knowing this we can simply not reset i and fact inside the loop. * 5.因此,知道了这一点,我们就无法在循环内重置ifact If we enter 4, after the first iteration, fact will be equal to 4!, and i will be equal to 5. If simply don't reset them, on the next iteration we will simply multiply fact (4!) by 5. Then i will become six and the while loop will terminate. 如果我们输入4,则在第一次迭代后, fact将等于4 !,而i将等于5。如果根本不重置它们,则在下一次迭代中,我们将fact (4!)乘以5。然后i将变成六岁, while循环将终止。 Then this will continue until the outer while loop terminates. 然后这将继续,直到外部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++;
}

With 4 as the input this will reduce the amount of iterations in the inner while loop from 99 to 14. 输入4可以将内部while循环中的迭代次数从99减少到14。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM