简体   繁体   English

我需要在课堂上显示学生的数字标记以及他们的字母成绩。 我的代码不输出字母。 我怎样才能解决这个问题?

[英]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?

import java.io.*;
class finalGrade
{
    //declare variables
    String name;
    double quizTotal = 0;
    double midterm = 0;
    double exam = 0;
    double mark = 0;
    char grade;


    //initialize variables
    public finalGrade (String nameStudent, double quizMark, double midtermMark, double examMark)
    {
        name = nameStudent;
        quizTotal = quizMark;
        midterm = midtermMark;
        exam = examMark;
    }

    public double marks()
    {
        mark = ((exam * 0.25) + (midterm * 0.25) + (quizTotal * 0.50));

        System.out.println((name) + ", your mark is " + (mark) + " and you get a(n) " + (grade));

        if (mark >= 90)
        {
            return (grade) = 'A';
        }
        else if (mark >= 80 && mark < 90)
        {
            return (grade) = 'B';
        }
        else if (mark >= 70 && mark < 80)
        {
            return (grade) = 'C';
        }
        else if (mark >= 60 && mark < 70)
        {
            return (grade) = 'D';
        }
        else if (mark < 60)
        {
            return (grade) = 'F';
        }
        else
        {
            return (grade) = 'F';
        }


    }
}



class finalGradeTester
{
    public static void main(String[] args)
    {
    InputStreamReader inStream = new InputStreamReader(System.in);
    BufferedReader sub = new BufferedReader(inStream);

    finalGrade Student1 = new finalGrade("Student", 89, 100, 21);
    finalGrade Student2 = new finalGrade("Student", 34, 21, 9);
    finalGrade Student3 = new finalGrade("Student", 89, 100, 21);
    finalGrade Student4 = new finalGrade("Student", 89, 100, 21);
    finalGrade Student5 = new finalGrade("Student", 89, 100, 21);

    Student1.marks();
    Student2.marks();

    }

}

This is supposed to calculate the mark, two tests worth 25%, and an exam worth 50%. 这应该计算分数,两次测试占25%,而一次考试占50%。 It works fine for the calculations but when displaying the letter grade it is blank. 它可以很好地用于计算,但是在显示字母等级时为空白。 I do not know how to fix this. 我不知道该如何解决。 The output I get when I run the code is: Student, your mark is 74.75 and you get a(n) Student, your mark is 24.5 and you get a(n) 运行代码时得到的输出是:学生,您的分数是74.75,您得到一个(n)学生,您的分数是24.5,您得到一个(n)

It does not display the letter grade. 它不显示字母等级。

You are printing before computing the grade . 您在计算grade之前先进行打印。 Also, there is no need to return anything here. 另外,这里不需要返回任何内容。

public double marks()
{
    mark = ((exam * 0.25) + (midterm * 0.25) + (quizTotal * 0.50));

    if (mark >= 90)
    {
        grade = 'A';
    }
    else if (mark >= 80 && mark < 90)
    {
        grade = 'B';
    }
    else if (mark >= 70 && mark < 80)
    {
        grade = 'C';
    }
    else if (mark >= 60 && mark < 70)
    {
        grade = 'D';
    }
    else if (mark < 60)
    {
        grade = 'F';//This must be E
    }
    else
    {
        grade = 'F';
    }
    System.out.println((name) + ", your mark is " + (mark) + " and you get a(n) " + (grade));

}

Also, there is no reason to compute mark and grade each time the method is called. 同样,每次调用该方法时都没有理由计算markgrade You can compute the grade with a private method and call it from the constructor. 您可以使用私有方法计算成绩,然后从构造函数中调用它。

that's because you return the mark, instead of including ot on the System.out also, no need to re-verify if mark is <90, as otherwise it would be an A. 这是因为您返回了标记,而不是在System.out上也没有包含ot,如果标记<90,则无需重新验证,否则它将是A。

Also, class name have uppercase while variable have lowercase 另外,类名具有大写字母,变量具有小写字母

import java.io.*;
class FinalGrade
{
    //declare variables
    String name;
    double quizTotal = 0;
    double midterm = 0;
    double exam = 0;
    double mark = 0;
    char grade;


    //initialize variables
    public FinalGrade (String nameStudent, double quizMark, double midtermMark, double examMark)
    {
        name = nameStudent;
        quizTotal = quizMark;
        midterm = midtermMark;
        exam = examMark;
    }

    public void marks()
    {
        mark = ((exam * 0.25) + (midterm * 0.25) + (quizTotal * 0.50));

        if (mark >= 90)
        {
            grade = 'A';
        }
        else if (mark >= 80)
        {
            grade = 'B';
        }
        else if (mark >= 70)
        {
            grade = 'C';
        }
        else if (mark >= 60)
        {
            grade = 'D';
        }
        else
        {
            grade = 'F';
        }

        System.out.println((name) + ", your mark is " + (mark) + " and you get a(n) " + (grade));




    }
}



class finalGradeTester
{
    public static void main(String[] args)
    {
    InputStreamReader inStream = new InputStreamReader(System.in);
    BufferedReader sub = new BufferedReader(inStream);

    FinalGrade student1 = new FinalGrade("Student", 89, 100, 21);
    FinalGrade student2 = new FinalGrade("Student", 34, 21, 9);
    FinalGrade student3 = new FinalGrade("Student", 89, 100, 21);
    FinalGrade student4 = new FinalGrade("Student", 89, 100, 21);
    FinalGrade student5 = new FinalGrade("Student", 89, 100, 21);

    student1.marks();
    student2.marks();

    }

}

暂无
暂无

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

相关问题 阅读 3 次考试。输出学生姓名、成绩、平均成绩和字母成绩。 嵌套循环, - Read in 3 exams.. outputting the student names, their grades, the average, and their letter grade. Nested Loops, Java - 如何在我的程序中显示我的每个成绩的字母成绩,而不仅仅是我的最后一个成绩? - Java - How can I display a letter grade for each of my grades in my program rather than just my last one? 我想用另一个字母替换一个字母。 “n”及以下的每个字母都有效,但上半部分不会改变字母。 有人可以解决这个问题吗? - I want to replace a letter with another letter. Every letter from "n" and under works, but the top half doesn't change letters. Can somebody fix this? 如何创建一个窗口来运行我的字母分级程序? - How do I create a window to run my letter grade program in? 我怎样才能让我的打印语句在打印平均分数和相关字母等级的地方工作? - How can I get my print statement to work to where it prints the average score and the correlating letter grade? 如何获得我的学生成绩计算器程序中类似成绩字母的总数 - how can i get the total number of similar grade letters in my student grade calculator program Searchview 仅识别大写字母。 但我想要大写和小写字母结果 - Searchview identifying only Uppercase letter. But I want both upper and lower case letter results 如何使用switch()语句将数字转换为字母等级? - How can I use a switch() statement to convert from a numeric to a letter grade? 为什么我在运行程序时显示代码中的所有字母等级选项? 我只想要显示正确的答案 - Why are all the letter grade options in my code being shown when I run the program? I only want the correct answer shown Java Grade 等级,字母等级到数字 - Java Grade class, letter grade to number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM