【问题标题】:Returning a value from a Method从方法返回值
【发布时间】: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,因为那是我的数组吗?

标签: java methods return


【解决方案1】:

以下代码似乎已修复它。您需要将值放入 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

【讨论】:

    【解决方案2】:

    您正在从用户那里获取String[] temp 数组中的温度输入,但在计算平均值时,您传递的数组int[] arr 没有任何值。因此结果为零。

    您通过br.readLine() 获得的值是字符串形式。如果要计算它们的平均值,则需要在调用方法之前将temp[] 中的所有值转换为整数并将它们存储在arr[] 中。

    或者您可以使用扫描仪直接输入int 值。提示:

    Scanner sc = new Scanner(System.in);
    arr[0] = sc.nextInt();
    

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 2013-02-12
      相关资源
      最近更新 更多