从方法返回值

精巧的人

我正在尝试编写代码以将值返回到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对象。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章