繁体   English   中英

我怎样才能让我的打印语句在打印平均分数和相关字母等级的地方工作?

[英]How can I get my print statement to work to where it prints the average score and the correlating letter grade?

 public static void main(String[] args)
    {

        // Defining the constants for min and max range
        final int minValue = -1;
        final int maxValue = 100;
        String message = "Welcome to Simple Gradebook!";

        promptForInt(message, minValue, maxValue);

        // Declaring variables for the loop & the sentinel variable
        int score = 0;
        boolean doneYet = false;

        do
        {
            // Input Validation
            if (score < minValue || score > maxValue)
            {
                System.err.printf("Invalid value. The acceptable range is"
                        + " between %d and %d\n"
                        + "Please try again\n", minValue, maxValue);
            }
            else
            {
                doneYet = true;
            }
        } while (doneYet == false);

    }

    public static int promptForInt(String message, int minValue, int maxValue)
    {
        // Declaring variables for the loop & the sentinel variable
        int sum = 0;
        int numStudents = 0;
        int score = 0;

        System.out.println(message);

        //Creating the sentinel loop
        do
        {
            System.out.printf("Enter the score for student #%d"
                    + "(or -1 to quit): ", numStudents);
            Scanner keyboard = new Scanner(System.in);
            score = Integer.parseInt(keyboard.nextLine());

            if (score != -1)
            {
                sum += score;
                numStudents += 1;
            }

        } while (score != -1);
        double avgScore = (double) sum / numStudents;
       
        //Passing method to this method to convert grade to letter
        convertToLetter(avgScore);
        System.out.println("The average score is: " + avgScore
                + " which equates to a " + avgScore);
        return 0;

    }

    public static char convertToLetter(double avg)
    {
        char avgScore = 0;
        // Identifying the ranges for the grade letter
        if (avgScore >= 90)
        {
            System.out.println("A");
        }
        else if (avg >= 80)
        {
            System.out.println("B");
        }
        else if (avg >= 70)
        {
            System.out.println("C");
        }
        else if (avg >= 60)
        {
            System.out.println("D");
        }
        else
        {
            System.out.println("F");
        }
        return avgScore;

    }
}

我怎样才能让我的打印语句在打印平均分数和相关字母等级的地方工作? 像这样……“平均分是90.5,相当于A”

由于某种原因,我的输入验证无法正常工作。 如果输入的数字小于 -1 或大于 100,则应给出错误消息并再次开始循环。

您没有对convertToLetter(avgScore)的结果做任何事情。

将结果分配给变量:

char grade = convertToLetter(avgScore);

然后在打印时引用该变量:

System.out.println("The average score is: " + avgScore
  + " which equates to a " + grade);
    

----

您在调用promptForInt(message, minValue, maxValue)promptForInt(message, minValue, maxValue)了类似的问题; 您需要将结果分配给一个变量:

score = promptForInt(message, minValue, maxValue);

您遇到的另一个主要错误是您的输入检查代码。 如果score不在范围内,则do while循环将无限循环,因为score的值不会在循环内的任何地方更改。

打印错误消息后添加此行:

score = promptForInt(message, minValue, maxValue);

----

通常,您必须意识到变量具有作用域 为在convertToLetter方法中声明的avgScore变量赋值对在 main 方法中声明的同名变量没有任何影响 - 它们是不同的变量,其范围仅限于声明它们的方法。

  1. 您没有从函数convertToLetter正确分配和返回成绩字母。
  2. 您没有打印函数convertToLetter的返回值。

编写如下所示的函数:

public static char convertToLetter(double avg) {
    char gradeLetter;
    // Identifying the ranges for the grade letter
    if (avg >= 90) {
        gradeLetter = 'A';
    } else if (avg >= 80) {
        gradeLetter = 'B';
    } else if (avg >= 70) {
        gradeLetter = 'C';
    } else if (avg >= 60) {
        gradeLetter = 'D';
    } else {
        gradeLetter = 'F';
    }
    return gradeLetter;
}

然后将打印语句更改为System.out.println("The average score is: " + avgScore + " which equates to a " + convertToLetter(avgScore));

此代码有效。 后的解释。

import java.util.Scanner;

public class Calculat {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        // Defining the constants for min and max range
        final int minValue = -1;
        final int maxValue = 100;

        String message = "Welcome to Simple Gradebook!";
        System.out.println(message);

        // Declaring variables for the loop & the sentinel variable
        int score = 0;
        int sum = 0;
        int numStudents = 0;

        while (true) {
            score = promptForInt(keyboard, (numStudents + 1), minValue, maxValue);
            if (score == -1) {
                break;
            }
            sum += score;
            numStudents++;
        }
        if (numStudents == 0) {
            System.out.println("No data received.");
        }
        else {
            double avgScore = (double) sum / numStudents;
            char letter = convertToLetter(avgScore);
            System.out.printf("The average score is: %.3f which equates to a %c%n",
                              avgScore,
                              letter);
        }
    }

    public static int promptForInt(Scanner keyboard, int numStudent, int minValue, int maxValue) {
        // Declaring variables for the loop & the sentinel variable
        int score = 0;
        boolean isValidScore = false;

        // Creating the sentinel loop
        do {
            System.out.printf("Enter the score for student #%d (or -1 to quit): ", numStudent);
            String line = keyboard.nextLine();
            try {
                score = Integer.parseInt(line);
                if (score < minValue || score > maxValue) {
                    System.out.printf("Invalid value. Acceptable range is between %d and %d%n",
                                      minValue,
                                      maxValue);
                }
                else {
                    isValidScore = true;
                }
            }
            catch (NumberFormatException xNumberFormat) {
                System.out.printf("Not a number: %s%n", line);
            }
        } while (!isValidScore);
        return score;
    }

    public static char convertToLetter(double avg) {
        char avgScore = 0;
        // Identifying the ranges for the grade letter
        if (avg >= 90) {
            avgScore = 'A';
        }
        else if (avg >= 80) {
            avgScore = 'B';
        }
        else if (avg >= 70) {
            avgScore = 'C';
        }
        else if (avg >= 60) {
            avgScore = 'D';
        }
        else {
            avgScore = 'F';
        }
        return avgScore;
    }
}

方法promptForInt()应该接受来自用户的单个分数并返回它。 它应该只返回一个有效的分数。

方法main()包含用于收集所有分数的循环。

您需要检查是否至少输入了一个分数,否则无法计算平均值。

方法convertToLetter()总是将零值avgScore ,本质上使avgScore成为空字符。

这是该程序的示例运行。

Welcome to Simple Gradebook!
Enter the score for student #1 (or -1 to quit): 66
Enter the score for student #2 (or -1 to quit): 66
Enter the score for student #3 (or -1 to quit): 68
Enter the score for student #4 (or -1 to quit): -1
The average score is: 66.667 which equates to a D

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM