如何将数组传递给将双精度型转换为字符串的方法?

时间:2019-03-11 03:31:06

标签: java

enter image description here我正在研究convertToLetterGrade部分。我想弄清楚如何将我的数组传递到字符串中,以便以字母形式输出最小和最大等级,这里的注释很有用,并且我还需要通过gpa才能为输出一个字母等级

公共类WesternGPACalc {

public static void main(String[] args) {

    //TODO:
    //declare an array to hold the grades you earned in your classes
    //feel free to use fake values
    //declare at least 4 grades
    //use a 0.0 - 4.0 scale...
    //4.0 = A, 3.5 = A/B
    //3.0 = B, 2.5 = B/C 
    //2.0 = C
    //1.0 = D
    //0.0 = F
    //for example...
    double[] grade = new double[4];
    grade[0] = 4.00; // A
    grade[1] = 2.50; // B/C
    grade[2] = 2.00; // C
    grade[3] = 1.00; // D


    //TODO:
    //use a method to print the min and max grades as letter grades
    //printGradeStats(grade);

    printGradeStats(grade);
    //TODO:
    //declare an array to hold the credits each class was worth
    //the indices must match the grade[i] indices for the class
    //for example...
    int[] credit = new int[4];
    credit[0] = 3;
    credit[1] = 3;
    credit[2] = 3;
    credit[3] = 3;        

    //TODO:
    //use a method to calculate and print the GPA as both
    //a number and a letter grade
    //pass both the credit and grade arrays to the method
    //printGPA(grade, credit);
    printGPA(grade, credit);
}

//TODO:
//Finish this method which will accept an array of grades
//and print the min and max letter grade
private static void printGradeStats(double[] grade){

    //TODO:
    //First determine the min and max letter grade...

    double minGrade = 0;
    double maxGrade = 0;
    for(int i = 0; i < grade.length; i++) {
        if(grade[i] < minGrade) {
            minGrade = grade[i];
        }
        if(grade[i] > maxGrade) {
            maxGrade = grade[i];
        }
    }
    //TODO:
    //convert the min and max grades to a letter grade...
    //using your convertToLetterGrade(grade) method
    //For example...
    //String maxLetterGrade = convertToLetterGrade(maxGrade);
    String maxLetterGrade = convertToLetterGrade(maxGrade);
    String minLetterGrade = convertToLetterGrade(minGrade);
    //TODO:
    //Output them... 
    System.out.println("Max grade earned in a class was " + maxLetterGrade);
    System.out.println("min grade earned in a class was " + minLetterGrade);
}

//TODO:
//Finish this method which will convert
//a grade on the 4.0 scale and return a letter grade
//Use the following scale...
//A = 4.0
//4.0 > A/B >= 3.5
//3.5 > B >= 3.0
//3.0 > B/C >= 2.5
//2.5 > C >= 2.0
//2.0 > D >= 1.0
//F < 1.0
private static String convertToLetterGrade(double grade){

    String letterGrade = "F";


    return letterGrade;

}


//TODO:
//Finish this method which will accept an array of grades and credits
//and print the cumulative GPA as a letter grade
private static void printGPA(double[] grade, int[] credit){

    //Recall...GPA is just a weighted average...
    //Cumulative GPA is the sum of all grade points -- grade[i] * credit[i]
    //divided by the sum of all credits[i]       
    double sumCredits = 0;
    //TODO:
    //Calculate cumulative GPA      


    for (int n = 0; n < credit.length; n++) {
        sumCredits += credit[n];
    }

    double gpa = ((grade[0] * credit[0]) + (grade[1] * credit[1]) + (grade[2] * credit[2]) + (grade[3] * credit[3])) / sumCredits;
    //TODO:
    //Output Cumulative GPA as both a number and a grade
    System.out.println("Cumulative GPA " + gpa);        
    System.out.println("Cumulative GPA as a letter grade: ");
}

}

2 个答案:

答案 0 :(得分:1)

您可以通过使用if-else块来实现此目的

private static String convertToLetterGrade(double grade){
        String letterGrade="";
        if(grade == 4.0){
            letterGrade="A";
        }else if(grade >=3.5 && grade < 4){
            letterGrade="A/B";
        }else if(grade >= 3.0 && grade < 3.5){
            letterGrade="B";
        }else if(grade >= 2.5 && grade < 3.0){
            letterGrade="B/C";
        }else if(grade >= 2.0 && grade < 2.5){
            letterGrade="C";
        }else if(grade >= 1.0 && grade < 2.0){
            letterGrade="D";
        }else if(grade < 1.0){
            letterGrade="F";
        }
        return letterGrade;
 }

答案 1 :(得分:0)

我看到您有一个答案,但是让我稍微增强一下代码。

在计算累积GPA时,您对总和进行硬编码。实际上,您可以使用与获取sumCredits相同的循环,这是正确的方法(否则,您只能检查/考虑4个元素/等级)。

double gpa = 0.0;
for (int n = 0; n < credit.length; n++) {
    gpa += grade[n] * credit[n];
    sumCredits += credit[n];
}

gpa /= sumCredits;

逻辑上的一件事,

    double minGrade = 5.0; // Or any max value, because you are going to find minimum values that this default one
    double maxGrade = 0;
    for(int i = 0; i < grade.length; i++) {
        if(grade[i] < minGrade) {
            // if you set `double minGrade = 0` this if never becode true since every grade is greater than `0`
            minGrade = grade[i];
        }
        if(grade[i] > maxGrade) {
            maxGrade = grade[i];
        }
    }