我正在编写一个程序,该程序读取天气数据的.txt文件,并且除其他事项外,还要记录一年中每一天的各种天气模式。
.txt文件具有365行各种值(一年中的每一天都有一行值)。每行都包含高温,低温和天气状态(格式为由六个1和0组成的String
,其中1表示当天发生了雾/雨/雪/冰雹/雷电/龙卷风)。例如,String 011000
表示同时下雨和下雪。
示例天气文件看起来像(换行符为空格):
高,低,天气字符串:
45,32.4,100000
43.2、35、100001
50.2,32.1 101101
我已经完成了跟踪weather.txt文件并跟踪年度最高和年度最低的程序的绝大部分,但是在计算每种类型(6种类型)的天气状况时遇到了麻烦。我意识到我并没有尽全力去解释,但是我的目标是对所有365个格式化的String值保持计数,每个String索引中有多少个1。因此,在查看上面的示例天气文件时,我的最终结果将是[3,0,1,1,0,2]。
在下面的粘贴方法中,我传入了String array
(我之前在程序中创建了String array
,其中包含所有365个String格式的值... [100001,100000,101101,...])。在此方法中,我创建了一个具有6个值的新tally array
。我正在尝试编写一个循环,该循环跟踪传入的weather array
(365个值),如果在所述索引处出现1,则增加tally array index
。最终的tally array
看起来像[101、31、3、218、42、101] ...组成用于样本输出的数字。
逻辑给我带来了很多麻烦。假设我已经尽力解释了这一点,那么谁能给我建议。
注意-NUMBER_OF_WEATHER_TYPES的类常量设置为6。
public static int[] getWeatherCounts(String[] weather) {
int[] tally = new int[6];
for (int i = 0; i < weather.length; i++) {
for (int j = 0; j < NUMBER_OF_WEATHER_TYPES; j++) {
if (weather[j].charAt(j) == 1) {
tally[j]++;
}
return tally;
}
}
return tally;
}
整个程序的上下文:
import java.util.*;
import java.io.*;
public class WeatherInfo {
public static final int DAYS_PER_YEAR = 365;
public static final int NUMBER_OF_WEATHER_TYPES = 6;
public static void main (String[] args) {
String firstArgs = args[0];
Scanner input = null;
if (args.length != 1) {
System.out.println("Error"); //Look more into this!!!!
} else {
try {
input = new Scanner(new File(firstArgs));
} catch (FileNotFoundException e) {
System.out.println("Error: " + e);
System.exit(1);
}
}
String lineDiscard = input.nextLine();
double[] highs = new double[DAYS_PER_YEAR];
double[] lows = new double[DAYS_PER_YEAR];
String[] weather = new String[DAYS_PER_YEAR];
for (int i = 0; i < DAYS_PER_YEAR; i++) {
input.next();
input.next();
highs[i] = input.nextDouble();
lows[i] = input.nextDouble();
weather[i] = input.next();
}
displayWeatherStatistics(highs, lows, weather);
}
public static void displayWeatherStatistics(double[] highs, double[] lows, String[] weather) {
double highTemp = Integer.MIN_VALUE;
double lowTemp = Integer.MAX_VALUE;
// for loop for highs
for (int i = 0; i < highs.length; i++) {
if (highs[i] > highTemp) {
highTemp = highs[i];
}
}
// for loop for lows
for (int i = 0; i < lows.length; i++) {
if (lows[i] < lowTemp) {
lowTemp = lows[i];
}
}
// printouts for the low and high temps of the year...need to fix this a bit
System.out.println("Highest Temp: " + highTemp + " (F)");
System.out.println("Lowest Temp: " + lowTemp + " (F)");
System.out.println(Arrays.toString(getWeatherCounts(weather)));
}
public static int[] getWeatherCounts(String[] weather) {
int[] tally = new int[6];
for (int i = 0; i < weather.length; i++) {
for (int j = 0; j < NUMBER_OF_WEATHER_TYPES; j++) {
if (weather[i].charAt(j) == 1) {
tally[j]++;
}
return tally;
}
}
return tally;
}
}
最佳答案
好吧,不看整个程序就很难分辨。但在我看来
if (weather[j].charAt(j) == 1) {
tally[j]++;
}
return tally;
应该
if (weather[i].charAt(j) == '1') {
tally[j]++;
}
// omit the return tally, we don't want to do that until the end