Java在2d数组字符串中查找最低值

时间:2014-08-21 14:19:30

标签: java arrays multidimensional-array

我有一个作业,我使用的是2d数组的字符串,其中包含学生姓名,课程/科目以及每个学生在每个课程中得到的分数。我想在每门课程中找到最低价值,例如在数学学生中得到78,学生b得到98,学生C得到55,学生D得到81。

我希望我的程序检查数组并找到数组中55的最低标记。

这是我正在使用的程序代码,但它没有给出我期望的确切结果:

String[][] resultCard =  new String[][]{ 
            { "", "A", "B", "C", "D", "E"},
            { "Maths", "78", "98","55","81","65" }, 
            { "Physics", "55", "65", "88", "80", "64"}, 
            { "Java", "73", "66", "69", "79", "79"},
            { "C#", "74", "78", "45", "73", "85"},
            { "Ethics", "80", "90", "60", "70", "80"}
        };

double intresultCard=0;
double resultCardAvg=0;
int highest = 0;
int temp=0;
int lowest=0;

for(row=1;row<2;row++){
    for(col=1;col<resultCard.length;col++){
        intresultCard += Integer.parseInt(resultCard[row][col]);
        resultCardAvg=intresultCard/5;
    }
}

for(row=1;row<2;row++){
    for(col=1;col<resultCard.length-1;col++){
        temp = Integer.parseInt(resultCard[row][col]);
        if(highest<temp){
            highest=temp;
        }
    }
}

for(row=1;row<2;row++){
    for(col=1;col<resultCard.length-1;col++){
        temp = Integer.parseInt(resultCard[row][col]);
        lowest = Integer.parseInt(resultCard[row][col+1]);
        if(lowest>temp){
            lowest=temp;
        }           
        System.out.println("The marks in temp are "+temp+ " & marks in lowest are "+lowest);
    }
}

2 个答案:

答案 0 :(得分:1)

尝试以下

        String[][] resultCard = new String[][] {
                { "", "A", "B", "C", "D", "E" },
                { "Maths", "78", "98", "55", "81", "65" },
                { "Physics", "55", "65", "88", "80", "64" },
                { "Java", "73", "66", "69", "79", "79" },
                { "C#", "74", "78", "45", "73", "85" },
                { "Ethics", "80", "90", "60", "70", "80" } };

        for (int i = 1; i < resultCard.length; i++) {
            int lowestMarks = Integer.MAX_VALUE;
            for (int j = 1; j < resultCard[i].length; j++) {
                lowestMarks = Math.min(lowestMarks,
                        Integer.parseInt(resultCard[i][j]));
            }
            System.out.println("Subject : " + resultCard[i][0]
                    + ", Lowest Marks : " + lowestMarks);
        }

输出:

Subject : Maths, Lowest Marks : 55
Subject : Physics, Lowest Marks : 55
Subject : Java, Lowest Marks : 66
Subject : C#, Lowest Marks : 45
Subject : Ethics, Lowest Marks : 60

答案 1 :(得分:0)

首先围绕第一个,第二个和第三个解析for循环的for循环:仅循环一次,因此不需要。其次为什么找到平均值,它与具体标记无关。第二个解析循环看起来不错(除了只循环一次),但为什么找到最高标记,如果你试图找到最低标记?
你也不是要扫描所有行,只扫描一行;意味着您只接收来自第一个主题的数据,导致程序无法找到所有类中的最低标记。 。 。

你可以做这样的事情:

String[][] resultCard =  new String[][]{ 
    { " ", "A", "B", "C", "D", "E"},
    { "Maths", "78", "98","55","81","65" }, 
    { "Physics", "55", "65", "88", "80", "64"}, 
    { "Java", "73", "66", "69", "79", "79"},
    { "C#", "74", "78", "45", "73", "85"},
    { "Ethics", "80", "90", "60", "70", "80"}
 };

 int lowest = Integer.parseInt(resultCard[1][1]), tempInt;
 for(int row = 1; row <= resultCard.length; row++)
    for(int s = 1; s < resultCard[0].length; s++) {
        tempInt = Integer.parseInt(resultCard[row][s]);
        if(lowest > tempInt) lowest = tempInt; 
    }

 System.out.println("The Lowest Mark Out of All Classes Is: " + lowest);


或者你可以为每个最低级别做这个:

for(int row = 1; row <= resultCard.length; row++) {
    lowest = resultCard[row][1];

    for(int s = 1; s < resultCard[0].length; s++) {
        tempInt = Integer.parseInt(resultCard[row][s]);
        if(lowest > tempInt) lowest = tempInt;
    }

    System.out.println("The Lowest Mark For " + resultCard[row][0] + " Is: " + lowest);
}


希望这会有所帮助:)

EDIT

static String[][] resultCard;
int average(int collumn) {
    if(collumn > resultCard.length) return 0;

    int average;
    for(int i = 1; i < resultCard[collumn].length; i++)
       average += Integer.parseInt(resultCard[collumn][i]);
    return average / resultCard[collumn].length;
}

您也可以为其他标记编辑执行相同的操作。 你可以这样打电话给他们:

import java.util.Scanner;
import java.util.InputMismatchException;
//. . .
Scanner s = new Scanner(System.in);
try {
    System.out.println(average(s.nextInt()));
}catch(InputMismatchException e){System.out.println("Incorrect Form of Input. . .");}

我不知道您当前的Java级别,所以可以理解上面列出的代码可能非常神秘;如果是这样,评论,我很乐意进一步解释:)