【发布时间】:2016-03-19 21:49:48
【问题描述】:
我目前正在为学校的作业处理一个问题集,我真的快要完成了,但是我遇到了一些编译错误。
问题集包括显示平均周数。温度、最高温度、最低温度以及一周中最热和最冷的日子。
目前我正在尝试做的是显示一周中最热的日子,如果我解决了这个问题,我可以很容易地找到一周中最冷的日子。
我在尝试编译包含以下内容的代码时遇到一些编译错误
不兼容的类型:int[] 无法转换为 int
错误:找不到符号
如果我能得到一些关于该做什么的指导,那就太好了,我现在迷路了。
public class test1
{
// 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 - @@@@@@@ ASK WHY IT THERE ARE SO MANY DECIMALS ON THE SIDE WHEN AVERAGE ALL 1's
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");
int[] maxTempList = searchTemp(tempList, maxTemp);
for(int i = 0; i < maxTempList.length; i++){
System.out.print("The hottest days of the week are: " +maxTempList[i]);
System.out.print(weekDay(tempList,maxTemp));
}
}
// Average the temperature
public static double avgTemp(int[] array)
{
int tempTotal = array[0];
// Total temperature values
for(int i = 0; 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)
{
int max = array[0];
// Check and replace max temp
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)
{
int min = array[0];
for(int i = 1; i < array.length; i++){
if(min > array[i])
min = array[i];
}
return min;
}
// Return days
public static String weekDay(int i, int[] array)
{
int[] displayWeekDay = searchTemp(array, i);
for(i = 0; i < displayWeekDay.length; i++){
String weekDay = "";
switch(i)
{
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesdays";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
}
}
return weekDay;
}
// Finds the index of the hottest/coldest days
public static int[] searchTemp(int[] temp, int key)
{
int count = 0;
for(int i = 0; i < temp.length; i++){
if(temp[i] == key)
count++;
}
int[] index = new int[count];
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;
}
}
【问题讨论】:
-
我建议您不要再犯学生错误,即专注于用户交互和输入,而忽略了开发 API 和思考计算应该如何工作。通过硬连线或简单的测试输入来获得正确的输入,然后担心用户将如何提供您需要的数据。