代码之家  ›  专栏  ›  技术社区  ›  user2268839

沉迷于计算成绩并显示结果的Java程序

  •  0
  • user2268839  · 技术社区  · 13 年前

    我正在参加Java初学者课程,他们要求我们:

    • 编写一个程序,根据用户输入的分数列表计算a、B、C、D和F的数量。

    • 输入所有分数后,程序必须显示 每个等级的统计数字的水平条形图,如操作部分所示。

    • 计数图必须为每个计数的单位显示一个“*”。

    • 当用户输入-1时,程序必须显示最终图形和 出口

    • 程序的输出必须显示提示和计数图 如上面的操作部分所示。

    • 您可以假设用户只会输入数字。

    操作:

    程序启动并提示您输入测试分数或结束 通过输入-1来执行程序。如下所示:

    Number of A's: 0
    Number of B's: 0
    Number of C's: 0
    Number of D's: 0
    Number of F's: 0
    Enter a score (%) or -1 to end: 90
    

    当您输入每个分数时,应用程序会决定分数是否为 A、B、C、D或F,并在字母等级计数中加一。就像 以下内容:

    Number of A's: 1
    Number of B's: 0
    Number of C's: 0
    Number of D's: 0
    Number of F's: 0
    Enter a score (%) or -1 to end: 95
    

    每次输入分数时,程序都会更新积分。如下所示:

    Number of A's: 2
    Number of B's: 0
    Number of C's: 0
    Number of D's: 0
    Number of F's: 0
    Enter a score (%) or -1 to end: -1
    

    输入完分数后,程序将显示一个水平 A、B、C、D和F的计数条形图。就像 以下内容:

    A's: **
    B's:
    C's:
    D's:
    F's:
    

    规格:

    数字等级字母等级

    大于或等于90安

    小于90但大于或等于80B

    小于80但大于或等于70摄氏度

    小于70但大于或等于60D

    低于60华氏度

    当程序结束时,显示分数、平均分数和最佳分数。

    例子:

    分数:2

    平均得分:92.5

    最佳得分:95.0

    我们还被要求用至少两种方法进行编码:一种方法有一个void,另一种方法返回一些东西。

    到目前为止,我只能统计输入的分数,但我似乎无法理解 记录所有输入 和 计算平均值 和 选出最好的分数

    这就是我到目前为止所拥有的:

        import java.util.*;
    
    
        public class ScoreTally {
            public static void main(String[] args) {
                 Scanner input = new Scanner(System.in);
    
    
                int aCount = 0;
                int bCount = 0;
                int cCount = 0;
                int dCount = 0;
                int fCount = 0;
                int scoreCount = -1;
    
                double score = 0;
    
    
    
                while (score != -1) {
                    if (score >= 90)
                        aCount++;
    
                    else if ((score < 90) && (score >= 80))
                        bCount++;
    
                     else if ((score < 80) && (score >= 70))
                        cCount++;
    
                    else if ((score < 70) && (score >= 60))
                        dCount++;
    
                    else if ((score < 60) && (score > 0))
                        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 F's: " + fCount);
                    System.out.print("Enter a score (%) or -1 to end: ");
                    score = input.nextDouble();
    
                    scoreCount++;
    
                }
    
                if (score == -1)
                    System.out.println("Number of scores: " + scoreCount);
    
            }
        }
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   willowherb    13 年前

    使用函数返回最大的数字以获得最佳分数。 还有另一个打印最终图形的功能。 这应该考虑到两个功能的需求。 此外,使循环成为出口控制的循环,因为它必须至少运行一次。

    作为一种编码实践,您不应该使用星形导入(importjava.util.*),而应该只使用您需要的内容。

    此外,在开始时将scoreCount设置为-1也做得很好。

    import java.util.Scanner;
    
    public class ScoreTally {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
    
        int aCount = 0;
        int bCount = 0;
        int cCount = 0;
        int dCount = 0;
        int fCount = 0;
        int scoreCount = -1;
    
        double score;
        double totalScore = 0;
        double bestScore = -1;
    
    
        do {
            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 F's: " + fCount);
            System.out.print("Enter a score (%) or -1 to end: ");
            score = input.nextDouble();
            if (score >= 90) {
                aCount++;
            } else if ((score < 90) && (score >= 80)) {
                bCount++;
            } else if ((score < 80) && (score >= 70)) {
                cCount++;
            } else if ((score < 70) && (score >= 60)) {
                dCount++;
            } else if ((score < 60) && (score > 0)) {
                fCount++;
            }
            scoreCount++;
            totalScore = totalScore + score;
            bestScore = greaterNumber(bestScore, score);
    
    
        } while (score != -1);
    
        printGraph('A', aCount);
        printGraph('B', bCount);
        printGraph('C', cCount);
        printGraph('D', dCount);
        printGraph('F', fCount);
        System.out.println("Number of scores: " + scoreCount);
        System.out.println("Average scores: " + totalScore / scoreCount);
        System.out.println("Best scores: " + bestScore);
    
    }
    
    public static void printGraph(char grade, int count) {
        System.out.print("Number of " + grade + "'s: ");
        for (int i = 0; i < count; i++) {
            System.out.print("*");
        }
        System.out.println();
    }
    
    public static double greaterNumber(double firstNum, double secondNum) {
        if (firstNum >= secondNum) {
            return firstNum;
        } else {
            return secondNum;
        }
    }
    

    }

        2
  •  0
  •   J.Miller    13 年前

    首先,您已经有了计算结果的代码。

            int aCount = 0;
            int bCount = 0;
            int cCount = 0;
            int dCount = 0;
            int fCount = 0;
            int scoreCount = -1;
    

    你需要的是另一个可以保存总分的字段,即。

    int totalScore = 0;
    

    在进入while循环之前,您需要提示输入第一个分数,否则它将无法正确运行第一次迭代。在结束时(或开始时),您需要更新 totalScore 使用该回合的输入,如果输入为-1,则不更新。

    平均分数将是 totalScore/scoreCount 你应该在完成while循环之后计算。

    为了获得最好的分数,你可以有另一个字段 int = maxScore

    您可以使用更新循环的每次迭代

    maxScore = Math.max(maxScore, score);

    Math.max返回两个数字中的最大值。

        3
  •  0
  •   prashant    13 年前

    这很简单。声明两个变量,一个用于具有avgScore,另一个用于程序的开头。

    double bestScore = 0.0;
    double avgScore = 0.0;
    double totalScore = 0.0;
    while (score != -1) {
                    totalScore = totalScore + score;
                    if(score > bestScore)
                        bestScore = score;  
                    if (score >= 90)
                        aCount++;
    
                    else if ((score < 90) && (score >= 80))
                        bCount++;
    
                     else if ((score < 80) && (score >= 70))
                        cCount++;
    
                    else if ((score < 70) && (score >= 60))
                        dCount++;
    
                    else if ((score < 60) && (score > 0))
                        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 F's: " + fCount);
                    System.out.print("Enter a score (%) or -1 to end: ");
                    score = input.nextDouble();
    
                    scoreCount++;
    
                }
    
                if (score == -1){
                    System.out.println("Number of scores: " + scoreCount);
                    avgScore = totalScore/scoreCount;
                    System.out.println("Best Score: " + bestScore);
                    System.out.println("Avg Score: " + avgScore);
                }
    

    此外,为了显示图表,您可以使用您的最终计数。