如何将数组传递给方法以将double转换为字符串?

问题描述 投票:1回答:2

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: ");
}

}

java
2个回答
0
投票

你可以通过使用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;
 }

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;

Logic中的一件事,

    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];
        }
    } 
热门问题
推荐问题
最新问题