我目前正在为学校的作业设置一个问题集,我真的很接近完成但是在运行我的程序时遇到了逻辑错误。
问题集包括显示平均周数。温度,最高温度,最低温度,以及最热和最冷的星期几(如果有多个显示那些)。
我目前遇到的问题是,如果有多天是最热或最冷的,我的程序不会显示一周中的几天,很可能是由于我的方法。 (星期)。
示例我想要的: 样本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 //这是我目前的问题。
我认为这个问题来自我的方法“周日”,很可能是因为它没有调用一周中的两个不同的日子,而是只调用一个,并且根据温度的多少显示x次与max或min相同。如果我得到一些指导,那就太好了,谢谢。
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;
}
}
答案 0 :(得分:1)
你的weekDay()方法基本上与searchTemp()方法做同样的事情。如果您查看weekDay(),您将在内部看到您正在调用searchTemp()。从weekDay()方法中删除searchTemp(),可能会执行以下操作:
public static String weekDay(int i)
{ int currentDay = 0;
if((i / 7) == 0){
currentDay = i;
}
else{
currentDay = i % 7;
}
String[] weekDay = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return weekDay[currentDay];
}
这假设你不关心它是什么月份,月份总是从星期日开始
答案 1 :(得分:1)
修改你的weekDay方法,如下所示,它将返回一个具有最高温度的工作日数组,然后你可以用它来打印那些日子:
public class DisplayWeekTempStat {
// 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
System.out.print("The hottest days of the week are: ");
String[] tempMaxWeekDay = weekDay(maxTemp, tempList);
for (int num = 0; num < tempMaxWeekDay.length; num++) {
if (tempMaxWeekDay[num] != null)
System.out.println(tempMaxWeekDay[num]);
}
// Display the coldest days of the week
System.out.println("\n The coldest days of the week are: ");
String[] tempMinWeekDay = weekDay(minTemp, tempList);
for (int num = 0; num < tempMinWeekDay.length; num++) {
if (tempMinWeekDay[num] != null)
System.out.println(tempMinWeekDay[num]);
}
}
// 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) {
String[] maxWeekDays = new String[7];
String[] weekDay = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int k = 0;
for (int j = 0; j < weekDay.length; j++) {
if (array[j] == i) {
maxWeekDays[k] = weekDay[j];
k++;
}
}
return maxWeekDays;
}
}