简体   繁体   English

java 用于在数组中存储阶乘的循环

[英]java for loop storing factorial in array

im trying to make a program that calculates factorial of 1 to 5 and store those values in an array size of 5我正在尝试制作一个程序来计算 1 到 5 的阶乘并将这些值存储在大小为 5 的数组中

import java.util.*;
public class factorialArray
{
    public static void main(String [] args)
    {
        int factorialA[] = new int[5];
        for (int i=1; i<6; i++)
        {
            int factorial = 1;
            for(int j=1; j<=i; j++)
            {
                factorial = factorial *j;
            }
            factorial = factorialA[i];
        }
        System.out.println(factorialA[3]);
    }
}

i want to print out third 4th index which is factorialA[3] if im understanding arrays correctly.如果我正确理解 arrays,我想打印出第三个第四个索引,即 factorialA[3]。 however im getting 0. and also if i have int factorialA[]= new int[5] i get array index out of bounds could i get some help please?但是我得到0.而且如果我有int factorialA [] = new int [5]我得到数组索引超出范围我可以得到一些帮助吗?

First of all, the change the condition of the loop from 首先,改变循环的条件

for (int i=1; i<6; i++)

to

for (int i=1; i<5; i++) // you could also do i=0

Then, this line 然后,这一行

factorial = factorialA[i];

This doesn't make any change in your array. 这不会对您的阵列进行任何更改。 I suppose you intended to do 我想你打算这样做

factorialA[i] = factorial;

Minor correction: 小修正:

public static void main(String [] args) { 
    int factorialA[] = new int[5]; 
    for (int i=0; i<5; i++) { 
       int factorial = 1; 
       for(int j=1; j<=i+1; j++) { 
          factorial = factorial *j; 
       } 
      factorialA[i] = factorial; 
    } 
    System.out.println(factorialA[3]); 
}

This should do the trick. 这应该可以解决问题。

The issue here is solved in the code below. 这里的问题在下面的代码中解决了。 The assignment factorial = factorialA[i] was not helping. 赋值factorial = factorialA [i]没有帮助。 You need to store the factorial value in the array which in this case will be done by factorialA[i] = factorial and also there is an indexing issue as arrays in Java is 0 index based. 您需要将factorial值存储在数组中,在这种情况下,factorialA [i] = factorial将完成,并且还存在索引问题,因为Java中的数组是基于0索引的。 When your code was run, I got ArrayIndexOutOfBounds exception. 运行代码时,我得到了ArrayIndexOutOfBounds异常。 That is fixed in the code below. 这在以下代码中得到修复。 Hope this helps :-) 希望这可以帮助 :-)

public static void main(String [] args)
{
    int factorialA[] = new int[5];
    for (int i = 0; i < 5; i++)
    {
        int factorial = 1;
        for(int j= 1; j <= i + 1; j++)
        {
            factorial = factorial * j;
        }
        factorialA[i] = factorial;
    }
    System.out.println(factorialA[3]);
}

I guess you are looking for this 我猜你在找这个

public class factorialArray {

    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5};
        for(int i=1;i<5;i++) {
            arr[i] = factorial(i); 
        }

        for(int i=1;i<5;i++) {
            System.out.println(arr[i]); 
        }

    }

    public static int factorial(int number){  
          int i,fact=1;  

          for(i=1;i<=number;i++){    
              fact=fact*i;    
          }    
          return fact;   
         }  
}

You need to do some changes which are generating errors .I have mentioned them in the code. 您需要做一些产生errors更改。我在代码中提到过它们。

Here you are storing the factorial of any number at that index of the array . 在这里,您将在array index处存储factorial of any numberfactorial of any number So if you need to store factorials still number N then you need to access array-elements indexed from 1-N So you need to have Array size of N+1 . 因此,如果您需要存储仍然为N factorials那么您需要访问从1-N索引的array-elements所以您需要具有N+1数组大小。

Another mistake you have made is here factorial = factorialA[i]; 你犯的另一个错误是factorial = factorialA[i]; . So here you don't manipulate value of Array-Elements at all which results in the initial array which has all elements as 0 . 所以在这里你根本不操纵Array-Elements值,这导致了所有元素为0的初始array So you need to change the assignment order here to factorialA[i] = factorial . 所以你需要在这里将赋值顺序更改为factorialA[i] = factorial

import java.util.*;
public class factorialArray
{
    public static void main(String [] args)
    {
        int factorialA[] = new int[6];      //Array-Size=Maximum Number+1; so here 5+1=6.
        for (int i=1; i<6; i++)
        {
            int factorial = 1;
            for(int j=1; j<=i; j++)
            {
                factorial = factorial *j;
            }
            factorialA[i] = factorial; //Here assignment should be done to the element of array.
        }
        System.out.println(factorialA[3]);
    }
}

I want to print out third 4th index which is factorialA[3] if im understanding arrays correctly. 我想打印出第三个第四个索引,即factorialA [3],如果我正确理解数组。 however im getting 0 但是我得到0

When you create an int array using int factorialA[] = new int[5] , the array elements will be initialized with 0 values which is what it is printing because you are NOT assigning the calculated factorial value to the array, so you need to correct it as shown below: 当你使用int factorialA[] = new int[5]创建一个int数组时, 数组元素将被初始化为0 ,这就是它打印的原因,因为你没有将计算出的factorial值赋给数组,所以你需要更正如下所示:

factorialA[i] = factorial; //assign calculated value to the array element

Also, array indexes start from 0 , so change the outer loop as: 此外,数组索引从0开始,因此将外部循环更改为:

for (int i=0; i<5; i++)

First thing if you want factorial up to n then initialise array of length n+1. 首先,如果你想要阶乘到n然后初始化长度为n + 1的数组。 Say you want up to factorial[5], then have array of size factorial[6]. 假设你想要阶乘[5],然后有大小因子数组[6]。 As array index starts from 0 goes up to n-1, where n is length of array. 当数组索引从0开始上升到n-1,其中n是数组的长度。

public class Factorial {

 public static void main(String[] args) 
 {
        int arr[] = {1,2,3,4,5};
        for(int i=0;i<5;i++) {
            arr[i] = factorial(arr[i]); 
        }

        for(int i=0;i<5;i++) {
            System.out.println(arr[i]); 
        }

  }

  public static int factorial(int number)
  {  
          int i,fact=1;  

          for(i=1;i<=number;i++)
          {    
              fact=fact*i;    
          }    
          return fact;   
   }  

} }

@fairoz answered the problem almost correctly. @fairoz 几乎正确地回答了这个问题。 They just passed the input i in the factorial class instead of arr[i], which will give the right answer他们只是在阶乘 class 中传递了输入 i 而不是 arr[i],这将给出正确的答案

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

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