by Anonymous » 26 Aug 2025, 09:03
Ich habe eine Textdatei mit durchschnittlichen Gaspreisen für jede Woche in einem Jahr, und ich muss die Daten verarbeiten, indem ich die monatlichen Durchschnittswerte dieser Preise erhalten kann. Diese Übung erfordert, dass ich die 52-wöchigen Durchschnittswerte in ein Array einfügen und die durchschnittlichen monatlichen Gaspreise aufweist. Es gibt 4 volle Wochen in einem Monat, aber der 52-Wochen-Datensatz bedeutet, dass jeder Monat eine zusätzliche Woche ausmachen kann.
Code: Select all
import java.io.*;
import java.util.Scanner;
public class GasPrices {
public static void main(String[] args) throws IOException {
// Create an array to hold the weekly gas price average.
double [] gasPrice = new double[52];
// Bring the file into the program.
File file = new File("1994_Weekly_Gas_Averages.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
for(int i = 0; i < gasPrice.length; i++) {
gasPrice[i] = inputFile.nextDouble();
}
}
// Print the monthly averages.
System.out.printf("Monthly average: %.2f", getAverage(gasPrice));
// Close the file.
inputFile.close();
}
/**
* The getAverage array takes the average of each month and returns the monthly average.
* @param array the yearly array prices.
* @return the average monthly price.
*/
public static double getAverage(double [] array) {
double total = 0;
for(int i = 0; i < array.length; i++) {
total += array[i];
}
return total / array.length;
}
< /code>
Aus diesem Code hier dauert die Methode "Getaal durchage" im Durchschnitt des gesamten Jahres und nicht von Monat zu Monat. Ich bin festgefahren, wie genau ich die Monate verteilten, um einen monatlichen Durchschnitt zu erhalten. Jede Zeile repräsentiert den wöchentlichen Gaspreis-Durchschnitt (Zeile 1 = Woche 1, Zeile 2 = Woche 2 usw.).0.992
0.995
1.001
0.999
1.005
1.007
1.016
1.009
1.004
1.007
1.005
1.007
1.012
1.011
1.028
1.033
1.037
1.04
1.045
1.046
1.05
1.056
1.065
1.073
1.079
1.095
1.097
1.103
1.109
1.114
1.13
1.157
1.161
1.165
1.161
1.156
1.15
1.14
1.129
1.12
1.114
1.106
1.107
1.121
1.123
1.122
1.113
1.117
1.127
1.131
1.134
1.125
Ich habe eine Textdatei mit durchschnittlichen Gaspreisen für jede Woche in einem Jahr, und ich muss die Daten verarbeiten, indem ich die monatlichen Durchschnittswerte dieser Preise erhalten kann. Diese Übung erfordert, dass ich die 52-wöchigen Durchschnittswerte in ein Array einfügen und die durchschnittlichen monatlichen Gaspreise aufweist. Es gibt 4 volle Wochen in einem Monat, aber der 52-Wochen-Datensatz bedeutet, dass jeder Monat eine zusätzliche Woche ausmachen kann.[code]import java.io.*;
import java.util.Scanner;
public class GasPrices {
public static void main(String[] args) throws IOException {
// Create an array to hold the weekly gas price average.
double [] gasPrice = new double[52];
// Bring the file into the program.
File file = new File("1994_Weekly_Gas_Averages.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
for(int i = 0; i < gasPrice.length; i++) {
gasPrice[i] = inputFile.nextDouble();
}
}
// Print the monthly averages.
System.out.printf("Monthly average: %.2f", getAverage(gasPrice));
// Close the file.
inputFile.close();
}
/**
* The getAverage array takes the average of each month and returns the monthly average.
* @param array the yearly array prices.
* @return the average monthly price.
*/
public static double getAverage(double [] array) {
double total = 0;
for(int i = 0; i < array.length; i++) {
total += array[i];
}
return total / array.length;
}
< /code>
Aus diesem Code hier dauert die Methode "Getaal durchage" im Durchschnitt des gesamten Jahres und nicht von Monat zu Monat. Ich bin festgefahren, wie genau ich die Monate verteilten, um einen monatlichen Durchschnitt zu erhalten. Jede Zeile repräsentiert den wöchentlichen Gaspreis-Durchschnitt (Zeile 1 = Woche 1, Zeile 2 = Woche 2 usw.).0.992
0.995
1.001
0.999
1.005
1.007
1.016
1.009
1.004
1.007
1.005
1.007
1.012
1.011
1.028
1.033
1.037
1.04
1.045
1.046
1.05
1.056
1.065
1.073
1.079
1.095
1.097
1.103
1.109
1.114
1.13
1.157
1.161
1.165
1.161
1.156
1.15
1.14
1.129
1.12
1.114
1.106
1.107
1.121
1.123
1.122
1.113
1.117
1.127
1.131
1.134
1.125
[/code]