如何在main方法上打印我的方法来显示输出?

时间:2014-11-29 05:45:41

标签: java

import java.util.Scanner;

public class Hw7Pr2 {

    public static void main(String[] args) {
        int[] grades = { 40, 55, 70, 58 };

        System.out.println("best: ");
        int best1 = best(grades);
        System.out.print(best1);

        // Print Grade
        System.out.println("Grade: ");
        char [] grade = (char[]) best(grades);
        for (char i = 0; i < grade.length; i++) {
            String output= " ";
            output += "Student " + i + " score is " +
            grades[i] + " and grade is " + grade + "\n";
        }



        }


    private static char gradeLetter(int[] grades) {
        char grade = 0;
        for (int i = 0; i < grades.length; i++) {
            if (grades[i] >= best(grades) - 10)

                grade = 'A';

            else if (grades[i] >= best(grades) - 20)

                grade = 'B';

            else if (grades[i] >= best(grades) - 30)

                grade = 'C';

            else if (grades[i] >= best(grades) - 40)

                grade = 'D';

            else

                grade = 'F';


        }

        return grade;
    }

    public static int best(int[] grades) {
        System.out.println("The best scores is: ");
        int best = grades[0];
        for (int i = 0; i < grades.length; i++) {
            if (grades[i] > best)
                best = grades[i];
        }
        return best;

    }
}

我试图像这样显示输出

学生1分为40分,分数为C. 学生2分为55分,分数为B. 学生3分为70分,分数为A. 学生4分为58分,分数为B

但是我在打印gradeLetter方法时遇到了问题。

3 个答案:

答案 0 :(得分:0)

char [] grade =(char [])best(grade);

它是您代码中的无效部分。你不能将int转换为char []数组。

答案 1 :(得分:0)

也许这就是你想要做的事情:

   public class Hw7Pr2 {
    public static void main(String[] args) {
        int[] grades = { 40, 55, 70, 58 };

        int best = best(grades);
        System.out.println("The best scores is: " + best);

        // Print Grade
        System.out.println("Grade: ");

        for (int i = 1; i <= grades.length; i++) {
            char grade = gradeLetter(grades[i-1], grades);
            String output = " ";
            output += "Student " + i + " score is " + grades[i-1] + " and grade is " + grade + "\n";
            System.out.println(output);
        }

    }

    private static char gradeLetter(int grade, int[] grades) {
        char charGrade = 0;
            if (grade >= best(grades) - 10)

                charGrade = 'A';

            else if (grade >= best(grades) - 20)

                charGrade = 'B';

            else if (grade >= best(grades) - 30)

                charGrade = 'C';

            else if (grade >= best(grades) - 40)

                charGrade = 'D';

            else

                charGrade = 'F';


        return charGrade;
    }

    public static int best(int[] grades) {

        int best = grades[0];
        for (int i = 1; i < grades.length; i++) { //will save a compare operation
            if (grades[i] > best)
                best = grades[i];
        }
        return best;
    }
}

答案 2 :(得分:0)

只是想告诉你其他一些看待它的方式......

import java.util.Scanner;

public class Hw7Pr2 {

    public static void main(String[] args) {
        // Search the array for min, max and average
        int[] grades = {40, 55, 70, 58};
        gradeStats(grades);
    }

    static void gradeStats(int[] array) {
        final int A = 90;
        final int B = 80;
        final int C = 70;
        final int D = 60;

        int minimumValue = 100;
        int maximumValue = 0;
        double gradeSum = 0;

        // Get max and min grades
        for (int i = 0; i < array.length; i++) {
            if (array[i] < minimumValue) {
                minimumValue = array[i];
            }

            if (array[i] > maximumValue) {
                maximumValue = array[i];
            }
        }

        // Get sum of grades
        for (int i = 0; i < array.length; i++) {
            gradeSum = gradeSum + array[i];
        }

        for (int i = 0; i < array.length; i++) {
            if (array[i] >= A) {
                System.out.println("Grade \"" + array[i] + "\" is an A.");
            } else if (array[i] >= B) {
                System.out.println("Grade \"" + array[i] + "\" is an B.");
            } else if (array[i] >= C) {
                System.out.println("Grade \"" + array[i] + "\" is an C.");
            } else if (array[i] >= D) {
                System.out.println("Grade \"" + array[i] + "\" is an D.");
            } else {
                System.out.println("Grade \"" + array[i] + "\" is an F.");
            }
        }

        System.out.println("The lowest grade is a " + minimumValue + ".");
        System.out.println("The highest grade is a " + maximumValue + ".");
        System.out.printf("The sum is %d.\n", (int) gradeSum);
        System.out.println("The average is " + gradeSum / array.length + ".");
    }
}