我想找到平均值最高的三个学生打印他们的名字和平均分。它似乎找到了所有三个地方中最高的。我知道可能有更好的方法来做到这一点,如果你选择解释一个更好的方法,请解释为什么这不起作用。只有结果中的最后一个语句不起作用。
结果是: 测试1的等级平均值为:88 测试2的等级平均值为:62 测试3的等级平均值为:79 亚历克斯的三次测试平均值是85 巴里的三次测试平均值是77 辛迪的三次测试平均值是81 Deb的三次测试平均值是72 埃里克的三次测试平均值是75 弗兰的三次测试平均值是63 加里的三次测试平均值是74 海伦的三次测试平均值是83 平均成绩最高的学生是:Alex,平均成绩为85分 平均成绩第二高的学生是:Alex,平均成绩为85分 平均成绩第三高的学生是:Alex,平均成绩为:85
我的代码是:
package lab3;
public class third {
public static void main(String[] args) {
String[] names = { "Alex", "Barry", "Cindy", "Deb", "Eric", "Fran",
"Gary", "Helen" };
int[][] grades = { { 77, 83, 96 }, { 88, 67, 78 }, { 92, 77, 76 },
{ 94, 42, 81 }, { 99, 54, 72 }, { 90, 46, 54 }, { 76, 59, 88 },
{ 94, 69, 88 } };
int S = grades.length; // number of students
int E = grades[0].length; // number of exams
int sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][0];
}
System.out.println("The class average for test 1 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][1];
}
System.out.println("The class average for test 2 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][2];
}
System.out.println("The class average for test 3 is: " + sum / S);
for (int a = 0; a < S; a++) {
sum = 0;
for (int b = 0; b < E; b++) {
sum = sum + grades[a][b];
}
System.out
.println(names[a] + "'s three test average is " + sum / 3);
}
int firstPlace = 0;
String firstPlaceName = "";
int secondPlace = 0;
String secondPlaceName = "";
int thirdPlace = 0;
String thirdPlaceName = "";
for (int u = 0; u < S; u++) {
sum = 0;
for (int t = 0; t < E; t++) {
sum = sum + grades[u][t];
}
if (sum > firstPlace) {
firstPlace = sum;
firstPlaceName = names[u];
secondPlace = firstPlace;
secondPlaceName = firstPlaceName;
thirdPlace = secondPlace;
thirdPlaceName = secondPlaceName;
}
if (sum > secondPlace && sum != firstPlace) {
secondPlace = sum;
secondPlaceName = names[u];
thirdPlace = secondPlace;
thirdPlaceName = secondPlaceName;
}
if (sum > thirdPlace && sum != firstPlace && sum != secondPlace) {
thirdPlace = sum;
thirdPlaceName = names[u];
}
}
System.out.println("The student with the highest average is: "+firstPlaceName+" with a average of: "+firstPlace/3);
System.out.println("The student with the second highest average is: "+secondPlaceName+" with a average of: "+secondPlace/3);
System.out.println("The student with the third highest average is: "+thirdPlaceName+" with a average of: "+thirdPlace/3);
}
}
我是初学者,只是学习控制结构,这就是为什么我这样做。
好的,所以我意识到我想要做的事情更简单但是,这仍然是不正确的。
我的新代码是: 包lab3;
public class third {
public static void main(String[] args) {
String[] names = { "Alex", "Barry", "Cindy", "Deb", "Eric", "Fran",
"Gary", "Helen" };
int[][] grades = { { 77, 83, 96 }, { 88, 67, 78 }, { 92, 77, 76 },
{ 94, 42, 81 }, { 99, 54, 72 }, { 90, 46, 54 }, { 76, 59, 88 },
{ 94, 69, 88 } };
int S = grades.length; // number of students
int E = grades[0].length; // number of exams
int sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][0];
}
System.out.println("The class average for test 1 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][1];
}
System.out.println("The class average for test 2 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][2];
}
System.out.println("The class average for test 3 is: " + sum / S);
for (int a = 0; a < S; a++) {
sum = 0;
for (int b = 0; b < E; b++) {
sum = sum + grades[a][b];
}
System.out
.println(names[a] + "'s three test average is " + sum / 3);
}
int firstPlace = 0;
String firstPlaceName = "";
int secondPlace = 0;
String secondPlaceName = "";
int thirdPlace = 0;
String thirdPlaceName = "";
for (int u = 0; u < S; u++) {
sum = 0;
for (int t = 0; t < E; t++) {
sum = sum + grades[u][t];
}
if (sum > firstPlace) {
firstPlace = sum;
firstPlaceName = names[u];
}
else if(sum>secondPlace){
secondPlace=sum;
secondPlaceName=names[u];
}
else if(sum>thirdPlace){
thirdPlace=sum;
thirdPlaceName=names[u];
}
else{
}
}
System.out.println("The student with the highest average is: "+firstPlaceName+" with a average of: "+firstPlace/3);
System.out.println("The student with the second highest average is: "+secondPlaceName+" with a average of: "+secondPlace/3);
System.out.println("The student with the third highest average is: "+thirdPlaceName+" with a average of: "+thirdPlace/3);
}
}
我的结果是: 测试1的等级平均值为:88 测试2的等级平均值为:62 测试3的等级平均值为:79 亚历克斯的三次测试平均值是85 巴里的三次测试平均值是77 辛迪的三次测试平均值是81 Deb的三次测试平均值是72 埃里克的三次测试平均值是75 弗兰的三次测试平均值是63 加里的三次测试平均值是74 海伦的三次测试平均值是83 平均成绩最高的学生是:Alex,平均成绩为85分 平均成绩第二高的学生是:Helen,平均成绩为83分 平均成绩第三高的学生是:Eric,平均成绩为:75
好的,我得到了一个有效的代码,我本可以添加更改地点但是以不同的方式进行。我找到它时,我将最高数字设置为0。 包lab3;
public class third {
public static void main(String[] args) {
String[] names = { "Alex", "Barry", "Cindy", "Deb", "Eric", "Fran",
"Gary", "Helen" };
int[][] grades = { { 77, 83, 96 }, { 88, 67, 78 }, { 92, 77, 76 },
{ 94, 42, 81 }, { 99, 54, 72 }, { 90, 46, 54 }, { 76, 59, 88 },
{ 94, 69, 88 } };
int S = grades.length; // number of students
int E = grades[0].length; // number of exams
int sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][0];
}
System.out.println("The class average for test 1 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][1];
}
System.out.println("The class average for test 2 is: " + sum / S);
sum = 0;
for (int a = 0; a < S; a++) {
sum = sum + grades[a][2];
}
System.out.println("The class average for test 3 is: " + sum / S);
double[] sg = new double[S];
for (int a = 0; a < S; a++) {
sum = 0;
for (int b = 0; b < E; b++) {
sum = sum + grades[a][b];
}
sg[a] = sum;
System.out
.println(names[a] + "'s three test average is " + sum / 3);
}
int firstPlace = 0;
String firstPlaceName = "";
int count = 0;
int remover = 0;
while (count != 3) {
count = count + 1;
remover = 0;
firstPlace = 0;
for (int u = 0; u < S; u++) {
sum = (int) sg[u];
if (sum > firstPlace) {
firstPlace = sum;
firstPlaceName = names[u];
remover = u;
}
}
System.out.println("The student with the" + "# " + count
+ " highest average is: " + firstPlaceName
+ " with a average of: " + firstPlace / 3);
sg[remover] = 0;
System.out.println(sg[remover]);
}
}
}
答案 0 :(得分:0)
所有这些if语句将连续检查;你真的希望他们都被检查吗?或者他们应该是否是ifs?
您可以阅读Java文档和教程中的if语句: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
更新:您可以删除最终的else语句,因为它什么都不做。在修复条件后,删除if语句中的其他赋值会导致您删除值。把
third = second;
second = first;
first = newHighest;
返回样式分配(这是伪代码,您需要修改它以实际适合您的变量)。还要考虑我列出的顺序,并问自己为什么要先完成最后一次作业。