简体   繁体   English

通过其他方法将计算值存储在数组中

[英]Storing Calculated values in an array from another method

I'm trying to make a program that calculates factorials from 0 to 19 in one method and storing those values in an array in another method (if that makes sense). 我正在尝试制作一个程序,用一种方法计算从0到19的阶乘,然后将这些值存储在另一种方法的数组中(如果有意义)。 This is my code so far, it works but I want to take the values in method long factorial (int number) in the void main(String[] args) method. 到目前为止,这是我的代码,它可以工作,但是我想在void main(String[] args)方法中使用long factorial (int number)方法long factorial (int number)中的值。 But I'm having trouble executing this. 但我在执行此程序时遇到了麻烦。 I've tried changing System.out.println (factorial(counter)) to System.out.println (**x** [factorial(counter)]) but that failed. 我尝试将System.out.println (factorial(counter))更改为System.out.println (**x** [factorial(counter)])但是失败了。 Sorry I'm new to programming and my English is not that good. 抱歉,我是编程新手,我的英语不太好。

    for (int counter = 0; counter <= 20; counter++){

         System.out.println (factorial(counter));

If you need to store results in the x array, then just assign on each iteration: 如果需要将结果存储在x数组中,则只需在每次迭代中分配:

for (int counter = 0; counter < size; counter++){
    x[counter] = factorial(counter);
    System.out.println(x[counter]);
}

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

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