我正在进行计算机编程课程,现在我们正在使用数组。我的目标是做到以下几点:
〜提示用户今年有多少数学考试。 〜询问用户在每次数学测试中的分数。 〜将每个分数放在一个数组中。 〜对数组进行排序,并将数学分数从最小到最大。 〜计算数学分数的平均值。
我的问题发生在它只是将得分保持在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 [line:1] 错误:令牌上的语法错误,删除这些令牌 文件:C:\ Users \ herhstudent \ Desktop \ gg.java [line:3] 错误:令牌上的语法错误&#34; void&#34;,@ expected 文件:C:\ Users \ herhstudent \ Desktop \ gg.java [line:3] 错误:令牌上的语法错误&#34; ...&#34; ,.预期 文件:C:\ Users \ herhstudent \ Desktop \ gg.java [line:3] 错误:令牌上的语法错误&#34; throws&#34;,接口预期 文件:C:\ Users \ herhstudent \ Desktop \ gg.java [line:6] 错误:令牌上的语法错误&#34 ;;&#34;,{此令牌后的预期 文件:C:\ Users \ herhstudent \ Desktop \ gg.java [line:44] 错误:语法错误,插入&#34;}&#34;完成InterfaceBody
答案 0 :(得分:1)
您的程序无法识别您的变量,因为您在循环内声明了它们。在循环内部声明的变量不存在于循环之外。试试这个:
int tesGrades = 0;
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
testGrades = k.nextInt();
}
此外,你执行此循环的方式,testGrades一直被覆盖,除了最后一个之外你没有任何输入。尝试:
testResults[i] = k.nextInt();
而不是:
testGrades = k.nextInt();
这将使用所有输入的测试分数填充testResults数组。
答案 1 :(得分:1)
这里有多个问题。第一个是在for循环中声明testGrades
:
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();
}
这意味着有a narrower scope。它以后不可用。你可以将它移动到for循环之前声明,但这仍然没有意义。这个变量应该代表总分,我们应该逐步增加它,而不是每次都设置它。我也重命名了它,因为它正在做的更明显:
int totalScore = 0;
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
totalScore += k.nextInt();
}
现在我们需要解决您从未实际向数组写入任何值的问题。这个循环现在变为:
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
testResults[i] = k.nextInt();
totalScore += testResults[i];
}
最后,最后我们不需要循环打印平均值。我们可以做一次。我们不需要变量testNum
,因为我们可以像以前一样使用testResults.length
。
System.out.println("The average of all your tests is " + (totalScore / testResults.length));
您遇到的最后一个问题是integer division。我会让你自己解决这个问题。
最终代码:
private static int[] testResults = new int[5];
public static void main(String... args) throws Exception
{
Scanner k = new Scanner(System.in);
int testGrades = 0;
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
testResults[i] = k.nextInt();
testGrades += testResults[i];
}
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();
System.out.println("The average of all your tests is " + (testGrades / testResults.length));
}
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;
}
}
}
}
答案 2 :(得分:0)
将第一个循环更改为:
for (int i = 0;i < testResults.length; i++)
{
System.out.print("Thank you, now please enter your grade on each test: ");
testResults[i] = k.nextInt(); // <--
}