【发布时间】:2014-12-09 03:40:26
【问题描述】:
我正在尝试编写代码以将值返回到 Main 类,但是当我这样做时,答案返回为 0.0 为什么会这样,我将如何解决它?
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[]temp = new String[7];
int[] arr = new int[7];
for (int i = 0; i < 7; i++) {
System.out.println("Please enter the temperature for the " + (i + 1)+" day of the week");
//Not the most gramatically correct. But i did what i could while using the loop.
temp[i] = br.readLine();
}
System.out.println("The temperature for Monday is: " + temp[0]);
System.out.println("The temperature for Tuesday is: " + temp[1]);
System.out.println("The temperature for Wednesday is: " + temp[2]);
System.out.println("The temperature for Thursday is: " + temp[3]);
System.out.println("The temperature for Friday is: " + temp[4]);
System.out.println("The temperature for Saturday is: " + temp[5]);
System.out.println("The temperature for Sunday is: " + temp[6]);
double avg = averageValue(arr);
System.out.println("Avg Temp for the week is: \t\t " + avg);
public static double averageValue(int[] arr) {
double average = 0;
for (int i = 0; i< arr.length; i++) {
average += arr[i]
}
return average / arr.length;
}
【问题讨论】:
-
arr 未在代码中初始化。
-
我可以将它切换到 temp,因为那是我的数组吗?