我希望有人可以帮助我。
我现在有一个在线Java OOP课程,我的作业差不多完成了,但是我遇到了一个问题,教授没有回复我的电子邮件。
我正在尝试制作一个程序,该程序使用包含日期和温度记录的文件,然后最终输出到一个单独的记录,该记录具有日期,当天的最高温度,当天的最低温度和平均温度那天。我们正在使用数组来跟踪数据。
大多数事情都可以正常工作,但是由于某些原因,某些日子将无法获得正确的最低温度,而是在程序开始时分配初始化数组索引的编号。
在此方面的任何帮助将不胜感激。下面是我的代码,以及程序运行后的数组外观,以及数组运行后的文件外观。
谢谢。
package dow.with.arrays;
import java.util.Arrays;
public class DOWWithArrays
{
public static void main(String[] args)
{ //start
InputFile inFile = new InputFile("input.txt");
OutputFile outFile = new OutputFile("output.txt");
//INITILIZATION
int day = 0;
int temp = 0;
int[] high = new int[8]; //declares an array of integers for high temps
int[] low = new int[8]; //declares an array of integers for low temps
int[] count = new int[8]; //declares an array of integers for counting days
int[] total = new int[8]; //declares an array of integers for total temp
for (day = 0; day < 8; day++) //initilization for the arrays
{
high[day] = -999;
low[day] = 999;
count[day] = 0;
total[day] = 0;
}
//tells user the DOW Temp program is starting
System.out.println("DOW Temperature Started. Please wait...");
System.out.println(Arrays.toString(high)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(low)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(count)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(total)); //GET RID OF THIS BEFORE TURN IN
while (!inFile.eof())
{ //not the end of file
day = inFile.readInt(); //read first int
temp = inFile.readInt(); //read second int
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
} else if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
count[day]++; //counts how many temps there are in the specific day
total[day] = total[day] + temp; //calculates the total temp for each day
} //now end of file
for (day = 1; day < 8; day++)
{
outFile.writeInt(day); //write day #
outFile.writeInt(high[day]); //write high temp for that day
outFile.writeInt(low[day]); //write low temp for that day
outFile.writeInt(total[day] / count[day]); //write average temp for that day
outFile.writeEOL(); //write end of line
System.out.println(day);
} //for
System.out.println(Arrays.toString(high)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(low)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(count)); //GET RID OF THIS BEFORE TURN IN
System.out.println(Arrays.toString(total)); //GET RID OF THIS BEFORE TURN IN
outFile.close();
System.out.println("DOW Temperature Completed Sucessfully.");
} //stop
} //end DOW With Arrays
之前:
[-999, -999, -999, -999, -999, -999, -999, -999]
[999, 999, 999, 999, 999, 999, 999, 999]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]`
之后:
[-999, 62, 56, 70, 61, 59, 77, 55]
[999, 55, 999, 63, 59, 999, 999, 999]
[0, 2, 2, 3, 2, 2, 1, 1]
[0, 117, 110, 200, 120, 108, 77, 55]
之前的文件:
1 62
1 55
2 54
2 56
3 67
3 70
3 63
4 61
4 59
5 49
5 59
6 77
7 55
之后的文件:
1 62 55 58
2 56 999 55
3 70 63 66
4 61 59 60
5 59 999 54
6 77 999 77
7 55 999 55
答案 0 :(得分:3)
If - Else if
...如果if
表达式为true
,则else if
被忽略。我认为错误在这里:
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
} else if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
尝试删除else
:
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
}
if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
答案 1 :(得分:1)
您已经有一个answer from Frighi;我只想指出一些替代方法。
当您第一次遇到一天时,当前代码就会出现问题,因为该代码不会同时高低更新。
您可以通过查看count
一天来明确地检测到这一点:如果为零,请同时更新两者:
if (count[day] == 0) {
low[day] = high[day] = temp;
} else if (temp > high[day]) {
high[day] = temp;
} else if (temp < low[day]) {
low[day] = temp;
}
或者,您可以简单地使用min
和max
,并且没有(明确的)条件:
high[day] = Math.max(high[day], temp);
low[day] = Math.min(low[day], temp);
答案 2 :(得分:0)
问题是else
;删除它:
if (temp > high[day]) {
high[day] = temp;
}
if (temp < low[day]) {
low[day] = temp;
}
请考虑一下,如果一天中只有1个温度,那么它既是最高温度,又是最低温度。
答案 3 :(得分:0)
您遇到的问题是确定一天中最低和最高温度的算法。具体来说,这部分:
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
} else if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
应该是:
if (temp > high[day]) //assigns the current highest temperature
{ //into the correct place in the high array
high[day] = temp;
}
if (temp < low[day])//assigns the current lowest temperature
{ //into the correct place in the low array
low[day] = temp;
}
如果您只是通过调试器来跟踪代码中的内容或更好的代码,则可能是您自己想出来的,对于第一种行为是错误的情况,第2天,或更简单的一天, 7。
另外,对于代码的这一部分,请研究Math.min
/ Math.max
函数,以及Integer.MIN_VALUE
/ Integer.MAX_VALUE
和+=
。
此外,出于可维护性和可扩展性的原因,您应该尝试找到一种不对程序中的天数进行硬编码的方法。或第一天是1的事实。