我正在尝试创建一个代码以将值返回到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;
}
答案 0 :(得分:0)
您正在获取用户对String[] temp
数组中温度的输入,但在计算平均值时,您传递的数组int[] arr
没有任何值。因此它出现为零。
br.readLine()
获得的值是字符串形式。如果要计算它们的平均值,您需要将temp[]
中的所有值转换为整数,并在调用方法之前将它们存储在arr[]
中。
或者您可以使用扫描仪直接输入int
值。提示:
Scanner sc = new Scanner(System.in);
arr[0] = sc.nextInt();
答案 1 :(得分:0)
以下代码似乎修复了它。您需要将值放入arr对象。
public static void main(String[] args) {
try {
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();
arr[i] = Integer.parseInt(temp[i]); // **
}
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);
} catch (Exception e) {
e.printStackTrace();
}
}
public static double averageValue(int[] arr) {
double average = 0;
for (int i = 0; i < arr.length; i++) {
average += arr[i];
}
return average / arr.length;
}
输出
Please enter the temperature for the 1 day of the week
1
Please enter the temperature for the 2 day of the week
2
Please enter the temperature for the 3 day of the week
3
Please enter the temperature for the 4 day of the week
6
Please enter the temperature for the 5 day of the week
7
Please enter the temperature for the 6 day of the week
8
Please enter the temperature for the 7 day of the week
9
The temperature for Monday is: 1
The temperature for Tuesday is: 2
The temperature for Wednesday is: 3
The temperature for Thursday is: 6
The temperature for Friday is: 7
The temperature for Saturday is: 8
The temperature for Sunday is: 9
Avg Temp for the week is: 5.142857142857143