gpt4 book ai didi

java - 有没有办法在另一种方法中使用随机数组方法中的现有变量?

转载 作者:行者123 更新时间:2023-11-30 10:36:46 25 4
gpt4 key购买 nike

我想将在我的 randomRainMonth() 方法中生成的变量用于另一个方法 totalRain() 中的计算。但是通过在我的 totalRain() 中使用 double[] totalsum = randomRainMonth(list); 它只会生成一组新的随机变量,因为它会再次调用该方法。谁能帮忙?

public class RainFall{

public static void main (String[] args){

double [] monthRain = new double[12];
String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
randomRainMonth(monthRain);
System.out.println("-------");
totalRain(monthRain, monthName);
// printMonths(monthName);
}

public static String[] printMonths(String list[]){
String[] pM = new String[list.length];
for (int i = 0; i<list.length; i++) {
//System.out.println(list[i]);
}
return pM;
}

public static double[] randomRainMonth(double list[]){
double[] rdMonth = new double[list.length];

for(int i = 0; i < list.length; i++){
rdMonth[i] = (int)(Math.random() * 100);
//System.out.println(rdMonth[i]);
}
return rdMonth;
}

public static double[] totalRain(double list[], String list2 []){

String[] mnth = printMonths(list2);
double[] totalsum = randomRainMonth(list);

double sum = 0;
for( double i : totalsum) {
sum += i;
}


for (int i = 0; i<list2.length; i++) {

System.out.print(mnth[i] +": ");
System.out.println(totalsum[i]);
}
System.out.println("Total: ");
System.out.println(sum);

return totalsum;

}
}

[已解决]最终代码:

public class RainFall{

public static void main (String[] args){


double[] monthRain = randomRainMonth();
String[] monthName = printMonths();
// randomRainMonth();
System.out.println("-------");
totalRain(monthRain, monthName);
// printMonths(monthName);
avgRainMonthly(monthRain);
}

public static String[] printMonths(){
String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i = 0; i<12; i++) {
//System.out.println(monthName[i]);
}
return monthName;
}

public static double[] randomRainMonth(){

double [] monthRain = new double[12];
for(int i = 0; i < 12; i++){
monthRain[i] = (int)(Math.random() * 100);
//System.out.println(monthRain[i]);
}
return monthRain;
}

public static double[] totalRain(double list[], String list2 []){

String[] mnth = list2;
double[] totalsum = list;

double sum = 0;
for( double i : totalsum) {
sum += i;
}


for (int i = 0; i<12; i++) {

System.out.print(mnth[i] +": ");
System.out.println(totalsum[i]);
}
System.out.println("Total: ");
System.out.println(sum);

return totalsum;

}

public static double[] avgRainMonthly(double list []){

double[] average = list;

double avg = 0;
double finAvg = 0;
for( double a : average) {
avg = avg + a;
finAvg = avg / 12;
}
System.out.println(finAvg);
return average;
}
}

最佳答案

您不应该在 randomRainMonth(monthRain); 方法调用之前创建 double [] monthRain = new double[12];,因为实际的数组对象需要由 randomRainMonth 方法创建和返回。

相反,您需要在main 中声明一个double[] 数组monthRain,然后收集 返回的结果randomRainMonth(monthRain) 如下:

//declare the double array
double[] monthRain = randomRainMonth();

randomRainMonth() 方法更改如下:

public static double[] randomRainMonth(){
//this method should be responsible to create the double array object
//and then return it to the caller after populating with elements
double [] monthRain = new double[12];

for(int i = 0; i < list.length; i++){
rdMonth[i] = (int)(Math.random() * 100);
//System.out.println(rdMonth[i]);
}
return monthRain;
}

现在,您可以将收集的数组(由 randomRainMonth 返回)传递给下一个方法,如下所示(您已经在做):

totalRain(monthRain, monthName);

关于java - 有没有办法在另一种方法中使用随机数组方法中的现有变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445626/

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