Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在列表中查找最高的数字。如果语句不起作用。有结果。JAVA_Java_Arrays_Variables_If Statement - Fatal编程技术网

在列表中查找最高的数字。如果语句不起作用。有结果。JAVA

在列表中查找最高的数字。如果语句不起作用。有结果。JAVA,java,arrays,variables,if-statement,Java,Arrays,Variables,If Statement,我想找到三个平均分最高的学生打印他们的名字和平均分。这三个地方似乎都是最高的。我知道可能有更好的方法可以做到这一点,如果你选择更好的解释方法,请先解释为什么这不起作用。只有结果中的最后语句不起作用 结果是: 测试1的班级平均成绩为:88 测试2的班级平均成绩为:62 测试3的班级平均成绩为:79 亚历克斯三次考试的平均成绩是85分 巴里三次考试的平均成绩是77分 辛迪三次考试的平均成绩是81分 Deb的三次测试平均值为72 埃里克三次考试的平均成绩是75分 弗兰的三次测试平均成绩是63 加里三次

我想找到三个平均分最高的学生打印他们的名字和平均分。这三个地方似乎都是最高的。我知道可能有更好的方法可以做到这一点,如果你选择更好的解释方法,请先解释为什么这不起作用。只有结果中的最后语句不起作用

结果是: 测试1的班级平均成绩为:88 测试2的班级平均成绩为:62 测试3的班级平均成绩为:79 亚历克斯三次考试的平均成绩是85分 巴里三次考试的平均成绩是77分 辛迪三次考试的平均成绩是81分 Deb的三次测试平均值为72 埃里克三次考试的平均成绩是75分 弗兰的三次测试平均成绩是63 加里三次考试的平均成绩是74分 海伦三次考试的平均成绩是83分 平均分最高的学生是:亚历克斯,平均分为:85 平均分第二高的学生是:亚历克斯,平均分为:85 平均分第三高的学生是:亚历克斯,平均分为: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包装;
公共三等舱{
公共静态void main(字符串[]args){
String[]name={“Alex”、“Barry”、“Cindy”、“Deb”、“Eric”、“Fran”,
“加里”、“海伦”};
int[][]等级={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;//学生人数
int E=成绩[0]。长度;//考试次数
整数和=0;
对于(int a=0;a第一名){
第一名=总和;
firstPlaceName=名称[u];
第二名=第一名;
secondPlaceName=firstPlaceName;
第三名=第二名;
thirdPlaceName=secondPlaceName;
}
如果(总和>第二名和总和!=第一名){
第二名=总和;
secondPlaceName=名称[u];
第三名=第二名;
thirdPlaceName=secondPlaceName;
}
如果(总和>第三名和总和!=第一名和总和!=第二名){
第三位=总和;
thirdPlaceName=名称[u];
}
}
System.out.println(“平均分最高的学生为:“+firstPlaceName+”,平均分为:“+firstPlace/3”);
System.out.println(“平均分第二高的学生为:“+secondPlaceName+”,平均分为:“+secondPlaceName/3”);
System.out.println(“平均分第三高的学生为:“+thirdPlaceName+”,平均分为:“+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);

    }
}
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]);
        }

    }
}
公共类第三{
公共静态void main(字符串[]args){
String[]name={“Alex”、“Barry”、“Cindy”、“Deb”、“Eric”、“Fran”,
“加里”、“海伦”};
int[][]等级={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;//学生人数
int E=成绩[0]。长度;//考试次数
整数和=0;
对于(int a=0;a第一名){
第一名=总和;
firstPlaceName=名称[u];
}
否则,如果(总和>第二名){
第二名=总和;
secondPlaceName=名称[u];
}
否则如果(总和>第三位){
third = second;
second = first;
first = newHighest;