ArrayList数据结构 - 每日温度

时间:2017-07-21 05:34:04

标签: java arraylist

我在堆栈溢出处到处都看了,我无法弄清楚我哪里出错了。请帮忙!非常感谢你的时间。

这是我的代码:

import java.util.ArrayList;
import java.util.Scanner;    

public class DailyTempandAverage {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    ArrayList<String> days = new ArrayList<>();
    ArrayList<Double> temperatures = new ArrayList<>();

    days.add("Monday");
    days.add("Tuesday");
    days.add("Wednesday");
    days.add("Thursday");
    days.add("Friday");
    days.add("Saturday");
    days.add("Sunday");

     System.out.print("Enter the temperature for: " +days);



    double sum = 0.0;
    for ( int i = 0; i < temperatures.size(); i++) {
        sum += temperatures.get(i);
    }

    double average = (double) sum / temperatures.size();

    System.out.println("Average temperatures = " + average);

    }
}

这是我的输出: 运行:

Enter the temperature for: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]Average temperatures = NaN
BUILD SUCCESSFUL (total time: 0 seconds)

这就是我需要做的事情,因为它没有做到这一切:

以每日温度的形式存储数据一周。您的程序应提示用户输入星期几(星期一到星期日)和每天的温度。将日期和温度存储在两个不同的ArrayLists中。程序的输出应提供每天的温度和每周平均值。

非常感谢任何帮助。我是Java新手,我有成为数据科学家的愿望。再次感谢有点陌生人。

3 个答案:

答案 0 :(得分:1)

您永远不会在temperatures中输入任何数据。

因此temperatures.size() == 0

除以0的任何内容都是NaN

因此,代码完全按照您的说法进行操作。

答案 1 :(得分:1)

您不使用扫描仪读取您的温度

        Scanner in = new Scanner(System.in);
        ArrayList<String> days = new ArrayList<>();
        ArrayList<Double> temperatures = new ArrayList<>();

        days.add("Monday");
        days.add("Tuesday");
        days.add("Wednesday");
        days.add("Thursday");
        days.add("Friday");
        days.add("Saturday");
        days.add("Sunday");

         for (String day : days) {
             System.out.println("Enter the temperature for: " + day);
             double temp = in.nextDouble();
             temperatures.add(temp);
         }

         double sum = 0.0;
         for ( int i = 0; i < temperatures.size(); i++) {
             sum += temperatures.get(i);
         }

         double average = (double) sum / temperatures.size();

         System.out.println("Average temperatures = " + average);

修改

虽然恕我直言,但我认为最好使用Map <String, Double>,这样你只需要一个收藏品

答案 2 :(得分:-1)

以下代码实现了所需的功能

public class Test {
static LinkedHashMap<String, Double> weekTemp;

public static void main(String args[]) {

    weekTemp = new LinkedHashMap<>();

    setWeekDays(); //This method sets all the days with default temperature 0.0
    storeTemperature(); //This method stores all the data provided by user
    calculateAvgTemp();//This method calculates average temperature and prints all days' temperature


}
private static void setWeekDays() {
    weekTemp.put("Monday", 0.0);
    weekTemp.put("Tuesday", 0.0);
    weekTemp.put("Wednesday", 0.0);
    weekTemp.put("Thursday", 0.0);
    weekTemp.put("Friday", 0.0);
    weekTemp.put("Saturday", 0.0);
    weekTemp.put("Sunday", 0.0);
}

private static void storeTemperature() {
    Scanner sc = new Scanner(System.in);
    for (String day : weekTemp.keySet()
            ) {
        System.out.print("Enter Temperature for " + day + " --> ");
        Double dayTemp = sc.nextDouble();
        weekTemp.put(day, dayTemp);
    }
}
private  static void calculateAvgTemp() {
    Double totalTemp = 0d;
    for (String s : weekTemp.keySet()) {
        System.out.println("Day--> " + s + " Temp--> " + weekTemp.get(s));
        totalTemp = totalTemp + weekTemp.get(s);
    }
    System.out.println("Average temperature of week --> " + totalTemp / 7);
}



}