简体   繁体   English

Java - 如何在我的程序中显示我的每个成绩的字母成绩,而不仅仅是我的最后一个成绩?

[英]Java - How can I display a letter grade for each of my grades in my program rather than just my last one?

In my assignment for college, I have to prompt for a grade and get user input 6 times, then display each mark with a letter grade alongside it, and then display the average.在我的大学作业中,我必须提示一个分数并让用户输入 6 次,然后在每个分数旁边显示一个字母分数,然后显示平均值。 I was finally able to get each mark to display right, but the letter grade for the last entry shows for every entry.我终于能够正确显示每个标记,但最后一个条目的字母等级显示为每个条目。 What do I need to change in my code for this to work?我需要在我的代码中进行哪些更改才能使其正常工作?

I have a method to return a letter grade, but the method only partially works;我有一种返回字母等级的方法,但该方法仅部分有效; like it displays an appropriate letter grade, but only for the last entry.喜欢它显示适当的字母等级,但仅适用于最后一个条目。

package com.company;

import java.util.Scanner; //Summons the Scanner class

public class Main { //Main class

    //Instance variables to count and calculate the grades
    public static double total;
    public static int grade;
    public static int entries = 0;

    public static void main(String[] args) { //Main method

        //Creates a scanner for user input
        Scanner keyboard = new Scanner(System.in);
        int[]grades = new int[7];

        while (entries < 6) { //Make the user enter 6 grades
            do {
                System.out.printf("Enter grade %d: ", entries); //Prompt for a grade
                grades[entries] = grade;
                entries++;
                grade = keyboard.nextInt(); //Get the user input
                total = total + grade; //Add grade to total
            } while (!wrongValue(grade)); //Call a method to prompt for a re-entry upon user error
        }
        grades[6]=grade;
        double average = total / 6; //Get the total
        System.out.printf("1st grade is %d which is %s%n%%", grades[1], letterGrade(grade));
        System.out.printf("2nd grade is %d which is %s%n%%", grades[2], letterGrade(grade));
        System.out.printf("3rd grade is %d which is %s%n%%", grades[3], letterGrade(grade));
        System.out.printf("4th grade is %d which is %s%n%%", grades[4], letterGrade(grade));
        System.out.printf("5th grade is %d which is %s%n%%", grades[5], letterGrade(grade));
        System.out.printf("6th grade is %d which is %s%n%%", grades[6], letterGrade(grade));
        System.out.printf("total average = %.1f%%", average);
    }

    public static String letterGrade (int grade)
    {
        if (grade >= 90 && grade <= 100)
            return "an A+";
        else if (grade >= 80 && grade <= 90)
            return "an A";
        else if (grade >= 75 && grade <= 80)
            return "a B+";
        else if (grade >= 70 && grade <= 75)
            return "a B";
        else if (grade >= 65 && grade <= 70)
            return "a C+";
        else if (grade >= 60 && grade <= 65)
            return "a C";
        else if (grade >= 55 && grade <= 60)
            return "a D+";
        else if (grade >= 50 && grade <= 55)
            return "a D";
        else
            return "an F";
    }
}

I suspect you wanted to call letterGrade with each grade you read from the user.我怀疑你想用你从用户那里读到的每个grade来调用letterGrade But first, arrays start at 0 (not 1 ).但首先, arrays 从0 (不是1 )开始。 Your format String (s) had odd %% literal(s) which would start over on the next line, and I cleaned up the code a bit (I'm not a fan of temporary variables that serve no purpose, nor do I like comments that fail to clarify the code).您的格式String (s) 有奇怪的%%文字,它将在下一行重新开始,我稍微清理了代码(我不喜欢无用的临时变量,我也不喜欢未能阐明代码的注释)。 Like,喜欢,

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int[] grades = new int[6];
    int total = 0;
    for (int i = 0; i < grades.length; i++) {
        do {
            System.out.printf("Enter grade %d: ", i + 1);
            grades[i] = keyboard.nextInt();
        } while (!wrongValue(grades[i])); 
        total += grades[i];
    }
    double average = total / 6.0;
    System.out.printf("1st grade is %d which is %s%n", grades[0], letterGrade(grades[0]));
    System.out.printf("2nd grade is %d which is %s%n", grades[1], letterGrade(grades[1]));
    System.out.printf("3rd grade is %d which is %s%n", grades[2], letterGrade(grades[2]));
    System.out.printf("4th grade is %d which is %s%n", grades[3], letterGrade(grades[3]));
    System.out.printf("5th grade is %d which is %s%n", grades[4], letterGrade(grades[4]));
    System.out.printf("6th grade is %d which is %s%n", grades[5], letterGrade(grades[5]));
    System.out.printf("total average = %.1f which is %s%n", average, 
            letterGrade((int) average));
}

Finally, I assumed based on your posted logic that wrongValue actually returns true when the value is valid (so the method name seems to be backwards).最后,我根据您发布的逻辑假设,当值有效时, wrongValue实际上返回true (因此方法名称似乎是向后的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个窗口来运行我的字母分级程序? - How do I create a window to run my letter grade program in? JAVA 正确格式化我的成绩簿,其中包含数组中包含的成绩 - JAVA Properly formatting my grade book containing grades contained in array 如何获得我的学生成绩计算器程序中类似成绩字母的总数 - how can i get the total number of similar grade letters in my student grade calculator program 我需要在课堂上显示学生的数字标记以及他们的字母成绩。 我的代码不输出字母。 我怎样才能解决这个问题? - I need to display a student's numerical mark in a class as well as their letter grade. My code does not output the letter. How can I fix this? 我怎样才能让我的打印语句在打印平均分数和相关字母等级的地方工作? - How can I get my print statement to work to where it prints the average score and the correlating letter grade? 如何在Java程序中显示带有行号的文件? - How can I display file with line numbers in my Java program? 如何让我的程序在我的子类而不是父类 class 中运行验证代码? - How can I make my program run the validation code in my sub-class rather than the parent class? 我应该如何在Java程序中显示代码? - How should I display my code inside of my Java program? 我无法在Java套接字程序中将多个传感器连接到服务器 - I can't connect more than one sensor to the server in my java socket program 如何让我的 java 程序在双击执行时使用我的自定义 JRE,而不是默认安装的 JDK? - How do I make my java program use my custom JRE when executed by double-clicking, rather than the default JDK installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM