【发布时间】:2018-04-24 16:23:13
【问题描述】:
我正在上计算机编程课程,现在我们正在使用数组。我的目标是做到以下几点:
~ 提示用户他们今年写了多少数学测试。 ~ 询问用户他们在这些数学测试中的分数。 ~ 将每个分数放在一个数组中。 ~ 对数组进行排序,并从最低到最高给出他们的数学分数。 ~ 计算数学分数的平均值。
我的问题发生在它只保持分数为 0 并且后来无法识别 testGrade int 变量,因此它不会对它们进行平均。
这是我现在的完整代码:
public static void main(String... args) throws Exception
{
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
int testGrades = k.nextInt();
}
System.out.print("The original sequence is: \n ");
for (int i = 0;i < testResults.length; i++)
{
System.out.print(testResults [i] + ", ");
}
System.out.println();
SortEm(testResults);
System.out.print("The new sequence is : \n ");
for (int i=0; i < testResults.length; i++)
{
System.out.print (testResults[i] + ", ");
}
System.out.println();
for (int i = 0;i < testResults.length; i++)
{
System.out.println("The average of all your tests is " + (testGrades / testNum));
}
}
private static void SortEm (int [] ar)
{
int temp;
for (int i = ar.length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (ar[j] > ar[j + 1])
{
temp = ar[j];
ar[j] = ar[j + 1];
ar[j+1] = temp;
}
}
}
}
我非常感谢您对正在发生的事情以及如何解决它的一些见解。 提前谢谢大家:)
回答后我的错误: 发现 6 个错误: 文件:C:\Users\herhstudent\Desktop\gg.java [行:1] 错误:标记上的语法错误,删除这些标记 文件:C:\Users\herhstudent\Desktop\gg.java [行:3] 错误:标记“void”的语法错误,@预期 文件:C:\Users\herhstudent\Desktop\gg.java [行:3] 错误:令牌“...”的语法错误,。预期的 文件:C:\Users\herhstudent\Desktop\gg.java [行:3] 错误:令牌“抛出”的语法错误,预期接口 文件:C:\Users\herhstudent\Desktop\gg.java [行:6] 错误:令牌“;”上的语法错误,{ 应在此令牌之后 文件:C:\Users\herhstudent\Desktop\gg.java [行:44] 错误:语法错误,插入“}”完成InterfaceBody
【问题讨论】:
-
how many times the array contains the value? 可能重复,做
testResults[i] = testGrades; -
testResults、testGrades、testNum和k在您提供的代码示例中均未定义