使用Java对数组中的条目进行排序和显示

时间:2014-03-24 02:06:58

标签: java sorting

我写了这个程序,将数字等级分为字母等级:

        int [] a = new int[20];
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter 20 Grades to be Counted:");

    for (int i = 0; i < a.length; i++)
    {
        a[i] = keyboard.nextInt();
    }


    for (int i = 0; i < a.length; i++)
    {
        if (a[i] >= 90)
        aCount++;

        else if(a[i] < 90 && a[i] >=80)
        bCount++;

        else if (a[i] < 80 && a[i] >= 70)
        cCount++;

        else if (a[i] < 70 && a[i] >=65)
        dCount++;

        else
        fCount++;
    }


    System.out.println("Number of A's: " + aCount);
    System.out.println("Number of B's: " + bCount);
    System.out.println("Number of C's: " + cCount);
    System.out.println("Number of D's: " + dCount);
    System.out.println("Number of FAILURES: " + fCount);
}

}

我想要做的是取入输入的成绩,并在各自的字母中显示实际的成绩。有点像这样:

一个的: 100 95 90

B的: 88 85

C'S: 79 77

......等等。怎么办呢?

1 个答案:

答案 0 :(得分:-1)

您可以使用集来存储各个成绩。要按排序顺序存储成绩,您可以使用TreeSet<Integer>。然后迭代集合。

    int [] a = new int[20];
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter 20 Grades to be Counted:");

    for (int i = 0; i < a.length; i++)
    {
        a[i] = keyboard.nextInt();
    }


    TreeSet<Integer> setA = new TreeSet<Integer>();
    TreeSet<Integer> setB = new TreeSet<Integer>();
    TreeSet<Integer> setC = new TreeSet<Integer>();
    TreeSet<Integer> setD = new TreeSet<Integer>();
    TreeSet<Integer> setF = new TreeSet<Integer>();
    for (int i = 0; i < a.length; i++)
    {
        if (a[i] >= 90){
            aCount++;
            setA.add(a[i]);
        }

        else if(a[i] < 90 && a[i] >=80){
            bCount++;
            setB.add(a[i]);
        }

        else if (a[i] < 80 && a[i] >= 70){
            cCount++;
            setC.add(a[i]);
        }

        else if (a[i] < 70 && a[i] >=65){
            dCount++;
            setD.add(a[i]);
        }

        else{
            fCount++;
            setF.add(a[i]);
        }
    }


    System.out.println("Number of A's: " + aCount);
    System.out.print("A's: ");
    for(Integer x : setA){
        System.out.print(x + " ");
    }
    System.out.println();
    System.out.println("Number of B's: " + bCount);
    System.out.print("A's: ");
    for(Integer x : setA){
        System.out.print(x + " ");
    }
    System.out.println();

    System.out.println("Number of C's: " + cCount);
    System.out.print("B's: ");
    for(Integer x : setB){
        System.out.print(x + " ");
    }
    System.out.println();
    System.out.println("Number of D's: " + dCount);
    System.out.print("C's: ");
    for(Integer x : setC){
        System.out.print(x + " ");
    }
    System.out.println();
    System.out.println("Number of FAILURES: " + fCount)
    System.out.print("D's: ");
    for(Integer x : setD){
        System.out.print(x + " ");
    }
    System.out.println();

编辑:如果是重复项,您可以使用ArrayList存储值,然后在显示结果之前使用Collections.sort对ArrayList进行排序。