【发布时间】:2016-03-20 15:39:06
【问题描述】:
我目前正在为学校的作业处理一个问题集,我真的快要完成了,但是在运行我的程序时遇到了逻辑错误。
问题集包括显示平均周数。温度、最高温度、最低温度以及一周中最热和最冷的日子(如果也有多个显示的话)。
我目前遇到的问题是,如果有多个日子是最热或最冷的,很可能是由于我的方法,我的程序不会显示星期几。 (工作日)。
例如我想要的: 示例 1:
输入一周内每天的最高温度(从周日开始):
11 21 15 12 21 15 9
一周平均气温:14.86度
本周最高气温:21度
本周最冷的温度是:9度
一周中最热的日子是:周一、周四 最冷的日子是:星期六
但是我遇到的问题是,当我尝试运行我的代码时会发生这种情况(从示例 1 复制相同的温度):
示例 2:
输入一周内每天的最高温度(从周日开始):
11 21 15 12 21 15 9
一周平均气温:14.86度
本周最高气温:21度
本周最冷的温度是:9度
一周中最热的日子是:MondayMonday // 这是我当前的问题。
我认为问题出在我的名为“weekDay”的方法中,很可能是它没有调用一周中的两个不同的日子,而是只调用一个,并且它会根据温度的多少显示 x 次与最大值或最小值相同。如果能得到这方面的指导就太好了,谢谢。
public class test2
{
// Main method
public static void main(String[] args)
{
// Create a new scanner
Scanner input = new Scanner(System.in);
// Set array list
int[] tempList = new int[7];
// Prompt user for input and store input
System.out.println("Enter the hightest temperature of each day for a week (starting on Sunday): ");
for(int i = 0; i < tempList.length; i++)
tempList[i] = input.nextInt();
// Averages temperature
double avgTemp = avgTemp(tempList);
System.out.printf("The average temperature of the week is: %.2f degree %n", avgTemp);
// Display hottest temperature
int maxTemp = maxTemp(tempList);
System.out.println("The highest temperature of the week is: " + maxTemp + " degree");
// Display coldest temperature
int minTemp = minTemp(tempList);
System.out.println("The coldest temperature of the week is: " + minTemp + " degree");
// Display hottest days of the week
int[] maxTempList = searchTemp(tempList, maxTemp);
System.out.print("The hottest days of the week are: ");
for(int i = 0; i < maxTempList.length; i++)
System.out.print(weekDay(maxTemp,tempList));
// Display the coldest days of the week
int[] minTempList = searchTemp(tempList, minTemp);
System.out.println("\n The coldest days of the week are: ");
for(int i = 0; i < minTempList.length; i++)
System.out.print(weekDay(minTemp,tempList));
}
// Average the temperature
public static double avgTemp(int[] array)
{
// Set a total temperature variable
int tempTotal = array[0];
// Add all temperature values
for(int i = 1; i < array.length; i++)
tempTotal = array[i]+tempTotal;
// Return temperature average.
return ((double)tempTotal/array.length);
}
// Get hottest temperature
public static int maxTemp(int[] array)
{
// Set hottest day variable
int max = array[0];
// Check and replace max temperature
for(int i = 1; i < array.length; i++){
if(max < array[i])
max = array[i];
}
return max;
}
// Get coldest temperature
public static int minTemp(int[] array)
{
// Set coldest day variable
int min = array[0];
// Check and replace coldtest temperature
for(int i = 1; i < array.length; i++){
if(min > array[i])
min = array[i];
}
return min;
}
public static String weekDay(int i, int[] array)
{
int[] displayWeekDay = searchTemp(array, i);
String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
for(int j = 0; j < displayWeekDay.length; j++){
int days = displayWeekDay[j];
return weekDay[days];
}
return null;
}
// Finds the index of the hottest/coldest days
public static int[] searchTemp(int[] temp, int key)
{
int count = 0;
// Searches the array for the index where the element value is the same
for(int i = 0; i < temp.length; i++){
// When the number is the same as the key, increase count
if(temp[i] == key)
count++;
}
// Create index array based on same number to the key
int[] index = new int[count];
// Copy index numbers of the key into index array
for(int j = 0; j < index.length; j++){
for(int i = 0; i < temp.length; i++){
if(temp[i] == key){
if(j > 0 && index[j - 1] == i)
continue;
else{
index[j] = i;
break;
}
}
}
}
return index;
}
}
【问题讨论】: