该程序从用户输入中获取10个温度,计算平均值,然后打印出高于平均值的温度。
出于某种原因,我没有看到,当我输入三个温度(按顺序为100,90,70)时,我得到了这个输出:
100 is an above average temperature.
100 is an above average temperature.
90 is an above average temperature.
但是当我按此顺序给出这个输入(70,90,100)时,我得到了
70 is not an above average temperature.
70 is an above average temperature.
90 is an above average temperature.
70 is an above average temperature.
90 is an above average temperature.
70 is not an above average temperature.
java.lang.ArrayIndexOutOfBoundsException: 2
at Lab9.main(Lab9.java:64)
有人可以帮我看看我不知道吗?
import java.util.Scanner;
public class Lab9
{
public static int[] temps, aboveAvgTemps;
public static int temp, totalTemp, avgTemp, currentTemp, oldTemp, numAboveAvg,
numOfEntries;
public static void main(String[] args)
{
System.out.println("Welcome to the above average temperature tester program.");
System.out.println("Please in an integer for the number of days you wish to measure.");
Scanner kb = new Scanner(System.in);
numOfEntries = kb.nextInt();
System.out.println("Please enter in " + numOfEntries + " temperatures.");
//initialize main array and variables
temps = new int[numOfEntries];
totalTemp = 0;
for(int i = 0; i < temps.length; i++)
{
System.out.println("Please enter in the temperature for day " + (i+1) + ":");
temp = kb.nextInt();
temps[i] = temp;
totalTemp += temp;
}
//final calculations
avgTemp = totalTemp/numOfEntries;
System.out.println("The average temperature is " + avgTemp);
//count up num of above avg temps
for(int i = 0; i < temps.length; i++)
{
if(temps[i] > avgTemp)
{
numAboveAvg++;
}
}
//initialize new array and variables for above avg temps
aboveAvgTemps = new int[numAboveAvg];
for(int i = 0; i < temps.length; i++)
{
if(temps[i] > avgTemp)
{
for(int j = 0; j <= i; j++)
{
aboveAvgTemps[j] = temps[j];
System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
}
}
else
{
System.out.println(temps[i] + " is not an above average temperature.");
}
}
}
}
答案 0 :(得分:0)
我不是你想用aboveAvgTemps数组做的。 此代码段中的for循环
if(temps[i] > avgTemp)
{
for(int j = 0; j <= i; j++)
{
aboveAvgTemps[j] = temps[j];
System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
}
}
造成了这个问题。如果你删除for循环并只打印“xx高于平均温度。”,你将得到所需的结果。
答案 1 :(得分:0)
我已经更改了最后一段代码:
//initialize new array and variables for above avg temps
aboveAvgTemps = new int[numAboveAvg];
int index = 0;
for(int i = 0; i < temps.length; i++)
{
if(temps[i] > avgTemp)
{
aboveAvgTemps[index] = temps[i];
System.out.println(aboveAvgTemps[index] + " is an above average temperature.");
index++;
}
else
{
System.out.println(temps[i] + " is not an above average temperature.");
}
}
答案 2 :(得分:0)
当找到高于1的平均温度时,带有“J”变量的for循环执行最多i次。这会导致多次打印和超出范围的异常。最后一个循环应该是
[INFO] Building Spark Project Examples 2.2.0-SNAPSHOT
[INFO]
------------------------------------------------------------------------
[WARNING] The POM for
org.apache.spark:spark-mllib_2.11:jar:2.2.0-SNAPSHOT is missing, no
dependency information available [WARNING] The POM for
org.apache.spark:spark-hive_2.11:jar:2.2.0-SNAPSHOT is missing, no
dependency information available [WARNING] The POM for
org.apache.spark:spark-streaming-flume_2.11:jar:2.2.0-SNAPSHOT is
missing, no dependency information available [WARNING] The POM for
org.apache.spark:spark-streaming-kafka-0-8_2.11:jar:2.2.0-SNAPSHOT is
missing, no dependency information available [INFO]
------------------------------------------------------------------------
[INFO] BUILD FAILURE [INFO]
------------------------------------------------------------------------
[INFO] Total time: 0.864 s [INFO] Finished at:
2017-02-15T18:02:07-05:00 [INFO] Final Memory: 17M/303M [INFO]
------------------------------------------------------------------------
[ERROR] Failed to execute goal on project spark-examples_2.11: Could
not resolve dependencies for project
org.apache.spark:spark-examples_2.11:jar:2.2.0-SNAPSHOT: The following
artifacts could not be resolved:
org.apache.spark:spark-mllib_2.11:jar:2.2.0-SNAPSHOT,
org.apache.spark:spark-hive_2.11:jar:2.2.0-SNAPSHOT,
org.apache.spark:spark-streaming-flume_2.11:jar:2.2.0-SNAPSHOT,
org.apache.spark:spark-streaming-kafka-0-8_2.11:jar:2.2.0-SNAPSHOT:
Could not find artifact
org.apache.spark:spark-mllib_2.11:jar:2.2.0-SNAPSHOT -> [Help 1]
[ERROR] [ERROR] To see the full stack trace of the errors, re-run
Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to
enable full debug logging. [ERROR] [ERROR] For more information about
the errors and possible solutions, please read the following articles:
[ERROR] [Help 1]
http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Process finished with exit code 1
答案 3 :(得分:0)
问题来自最后一个块,我改变了它:
int j = 0;
for (int i = 0; i < temps.length; i++) {
if (temps[i] > avgTemp) {
aboveAvgTemps[j] = temps[i];
System.out.println(aboveAvgTemps[j] + " is an above average temperature.");
j++;
} else {
System.out.println(temps[i] + " is not an above average temperature.");
}
}
答案 4 :(得分:0)
这显然是一项任务。当我在学校做这些时,我们总是要做的第一件事,以及我们的成绩的50%,是我们解决方案的伪代码。甚至在此之前,我认为对于新程序员来说,考虑他们在将手放到键盘之前试图解决的问题是非常重要的。从我收集到的任务是:
你有1和2;你遇到的问题是3.如果你在编码之前只说明你想要做什么,我认为一个好的描述是:
对于用户输入的每个温度,打印出温度高于平均温度&#39;如果它高于平均值,否则打印出温度低于平均值
考虑到这一点,将其转换为代码非常容易:
for(int i=0; i < i < temps.length; i++ ) { //for each temperature the user entered
if(temps[i] > avgTemp){//if the temperature is above the average
//print out the above average message
System.out.println(temps[i] + " is an above average temperature.");
}else{//otherwise if the temperature is below the average
//print out the below average message
System.out.println(temps[i] + " is a below average temperature.");
}
}
答案 5 :(得分:0)
function myFunction() {
//array
var temp = [temp[1], temp[2], temp[3]];
//number of elements
var t_num = temp.length;
//sum of elements
var t_sum = [t1, t2, t3].reduce(function(){return a + b;}, 0);
//avarage of elements
var t_avrg = t_sum / t_num;
//alert if > avarage
for (var i=0; i < t_num; i++){
if (temp[x] > t_avrg){
alert(temp[x]);
};
};
};