[英]Java assigning wrong number to Array
I hope someone can help me with this. 我希望有人可以帮助我。
I have an online Java OOP class right now, and I'm almost done with this assignment, but I've run into a problem, and the professor isn't answering my emails. 我现在有一个在线Java OOP课程,我的作业差不多完成了,但是我遇到了一个问题,教授没有回复我的电子邮件。
I'm trying to make a program that takes a file containing records of days and temperatures, which then eventually outputs to a separate record that has the day, highest temperature for that day, lowest temperature for that day, and average temperature for that day. 我正在尝试制作一个程序,该程序使用包含日期和温度记录的文件,然后最终输出到一个单独的记录,该记录具有日期,当天的最高温度,当天的最低温度以及当天的平均温度。 We're using arrays to keep track of our data.
我们正在使用数组来跟踪数据。
Most everything is working fine, but for some reason, some of the days will not get the correct lowest temperature, and will instead assign the number that the array index is initialized with at the beginning of the program. 大多数情况下一切正常,但是由于某些原因,有些日子将无法获得正确的最低温度,而是在程序开始时分配初始化数组索引的编号。
Any help on this would be appreciated. 任何帮助,将不胜感激。 Below is my code, as well as what the arrays look like after the program has run, and how the files look after the array has run.
下面是我的代码,以及程序运行后数组的外观,以及数组运行后文件的外观。
Thanks in advance. 提前致谢。
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
BEFORE: 之前:
[-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]`
AFTER: 后:
[-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]
FILE BEFORE: 之前的文件:
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
FILE AFTER: 之后的文件:
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
If - Else if
...if the if
expression is true
, the else if
is ignored. If - Else if
...如果if
表达式为true
,则else if
被忽略。 I think the mistake is here: 我认为错误在这里:
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;
}
try to remove else
: 尝试删除
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;
}
You already have an answer from Frighi ; 您已经从Frighi得到了答案 ; I just wanted to point out some alternatives.
我只想指出一些替代方案。
The problem with the current code comes when you first encounter a day, since the code will not update both low and high. 当前代码的问题是在您第一次遇到一天时出现的,因为代码不会同时高低更新。
You can detect this explicitly by looking at the count
for a day: if it is zero, update both: 您可以通过查看一天的
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;
}
Alternatively, you can simply use min
and max
, and no (explicit) conditions: 另外,您可以简单地使用
min
和max
,而没有(显式)条件:
high[day] = Math.max(high[day], temp);
low[day] = Math.min(low[day], temp);
The problem is the else
; 问题在于
else
; remove it: 去掉它:
if (temp > high[day]) {
high[day] = temp;
}
if (temp < low[day]) {
low[day] = temp;
}
Consider if there's only 1 temperature for the day, it will be both the highest and the lowest. 考虑一下,如果一天中只有1个温度,则温度既最高又最低。
The problem you have is with the algorithm for determining the lowest and highest temperature for a day. 您遇到的问题是确定一天中最低和最高温度的算法。 Specifically this part:
特别是这部分:
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;
}
Should be: 应该:
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;
}
You could have figured that out by yourself if you just followed the flow of the code in your head or better, with a debugger, for the first case where the behavior was wrong, day number 2. Or the simpler one, day number 7. 如果您只是通过调试器来跟踪代码中的内容,或者只是更好地使用调试器,则可能是您自己想出来的,对于第一种情况,行为是错误的,第2天,或者更简单的是第7天。
Also, for this part of the code specifically, look into Math.min
/ Math.max
functions and maybe Integer.MIN_VALUE
/ Integer.MAX_VALUE
and +=
. 另外,对于这部分代码,请查看
Math.min
/ Math.max
函数,以及Integer.MIN_VALUE
/ Integer.MAX_VALUE
和+=
。
Also, you should try to find a way not to hard-code the number of days in the program for maintainability and extendability reasons. 另外,出于可维护性和可扩展性的原因,您应该尝试找到一种不对程序中的天数进行硬编码的方法。 Or the fact that the first day is 1.
或第一天是1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.