2017-07-21 84 views
-6

我在堆棧溢出處找到無處不在,我無法弄清楚我要出錯的地方。請幫忙!非常感謝您的參與。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的新手,我渴望成爲一名數據科學家。再次感謝親切的陌生人。

回答

1

您從不輸入任何數據到temperatures

因此temperatures.size() == 0

任何除以0是NaN

因此,代碼是做什麼你告訴它。

+0

幸運約翰,只有你的答案沒有得到downvoted –

+0

@ScaryWombat對不起。我無法控制誰下了票。你是否暗示我要對所有的降價負責? (我不是),但如果我是這樣? - 我輸掉2分,就像我下調的那個人一樣。 – John3136

1

你不使用你的掃描儀在你的temparatures閱讀

嘗試

 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>以便只需要有一個收藏

-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); 
} 



}