本文介绍了用于分配寻求援助,以平均品位数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会复制/粘贴的分配,然后迄今为止我所取得的进展。我真的不找人做任务,只有一些援助,以点我在正确的方向,以我要去哪里错了吗?该程序运行时没有错误,但是它返回一个学生多个结果时,它应该只返回一个。我可以修改的档次和有它产生不同的结果,所以我知道数学正在做,但是我不知道为什么它给多个(不同),结果一名学生。我已经快要超过code并不能确定我要去哪里错了。我最好的猜测是,它的某处OutputGrade方法,我只是找不到它。

I'll copy/paste the assignment, and then the progress I've made thus far. I'm truly not looking for someone to do the assignment, only some assistance to point me in the right direction as to where I'm going wrong? The program is running without error, however it's returning multiple results for a student when it should only be returning one. I can modify the grades and have it produce different results, so I know the math is being done, however I don't know why it's giving multiple (and different) results for a single student. I have been going over the code and cannot determine where I'm going wrong. My best guess is that it's somewhere in the OutputGrade method, I just can't find it.

说明:

创建一个名为学生用下面的实例变量的Java类:
•私人字符串studentName;
•私人诠释[]等级; //档次的数组
包括setter和getter方法​​来设置和获取这2个属性。 )setGrades( - - 对等级例如数组变量的setter方法​​应该采取一个参数是已填写成绩一个int数组setGrades()应使用if语句(S),以确保每个等级值。在数组参数是有效的(0到100之间) - 你需要结合使用循环用if语句(多个)以有效地做到这一点。如果等级为出界,setGrades()应该是等级值设置为0,你传递给setGrades()数组必须3和5等级之间举行。
包括一个名为outputGrade()方法,平均占学​​生成绩阵列中的所有成绩,然后使用switch语句根据以下标准输出级转换:
•对于值在0-59,你的程序应该输出:学生已经失败这一类
•对于值在60-69,你的程序应该输出:学生得到了D
•对于值在70-79,你的程序应该输出:学生得到了C
•对于值在80-89,你的程序应该输出:学生得到一个B
•对于从90-100的价值观,你的程序应该输出:学生得到一个A
其中,学生是学生的实际名称。
创建一个名为TestStudent测试类实例化3名学生,并将他们的名字和成绩,然后为每个学生要求outputGrade()。为了您的3名学生,1必须有3个等级,1必须有4个档次,1必须有5个等级。这样,您将有3个不同大小的数组传递给setGrades()。确保1学生,setGrades()被调用,1级的值超出范围(小于0或大于100)。的

Create a Java class called student with the following instance variables:• private String studentName;• private int [] grades; //an array of gradesInclude setter and getter methods to set and get these 2 properties. The setter method for the grades instance array variable — setGrades() — should take a single argument which is an array of int with the grades already filled in. setGrades() should be used if statement(s) to make sure that each grade value in the array parameter is valid (between 0 and 100) — you will need to use a loop in conjunction with the if-statement(s) to do this efficiently. If a grade is out of bounds, setGrades() should set that grade value to 0. The array that you pass to setGrades() must hold between 3 and 5 grades.Include a method called outputGrade() that averages up all the grades in the student grades array and then uses a switch statement to output grade conversions based on the following criteria:• For values from 0–59, your program should output: "Student has failed this class"• For values from 60–69, your program should output: "Student gets a D"• For values from 70–79, your program should output: "Student gets a C"• For values from 80–89, your program should output: "Student gets a B"• For values from 90–100, your program should output: "Student gets an A"Where "Student" is the actual name of the student.Create a test class called TestStudent that instantiates 3 students and sets their names and grades and then calls outputGrade() for each student. For your 3 students, 1 must have 3 grades, 1 must have 4 grades, and 1 must have 5 grades. This is so you will have 3 different array sizes to pass to setGrades(). Make sure that for 1 student, setGrades() is called with 1 grade value that is out of bounds (less than 0 or greater than 100).

code:

public class TestStudent {

    public static void main(String[] args) {

        Student student1 = new Student();
        int[] grades1 = {15, 50, 5};
        student1.setStudentName("Ernest Craft");
        student1.setGrades(grades1);
        student1.outputGrades();

        Student student2 = new Student();
        int[] grades2 = {95, 95, 95, 95};
        student2.setStudentName("Steve Jones");
        student2.setGrades(grades2);
        student2.outputGrades();

        Student student3 = new Student();
        int[] grades3 = {105, -1, 72, 90, 88};
        student3.setStudentName("Mary Smith");
        student3.setGrades(grades3);
        student3.outputGrades();

    } // end method main
} // end class TestStudent

Student类:

Student class:

public class Student {

    private String studentName;
    private int[] grades;

    //constructor
    public Student() {
    }

    public void setStudentName(String name) {
        studentName = name;
    } // end method setStudentName

    public String getStudentName() {
        return studentName;
    } // end method getStudentName

    public void setGrades(int gradeArray[]) {
        grades = gradeArray;

        for (int i = 0; i < gradeArray.length; i++) {
            if (gradeArray[i] < 0 || gradeArray[i] > 100) {
                gradeArray[i] = 0;
            } // end if
        } // end loop
    } // end method setGrades

    public int[] getGrades() {
        return grades;
    } // end method getGrades

    public void outputGrades() {

        int gradesTotal = 0;
        int gradesAverage;
        char letterGrade;

        for (int i : grades) {
            gradesTotal += i;
        } // end loop

        gradesAverage = gradesTotal / (grades.length);

        if (gradesAverage >= 0 && gradesAverage <= 59) {
            letterGrade = 'F';
        } else if (gradesAverage >= 60 && gradesAverage <= 69) {
            letterGrade = 'D';
        } else if (gradesAverage >= 70 && gradesAverage <= 79) {
            letterGrade = 'C';
        } else if (gradesAverage >= 80 && gradesAverage <= 89) {
            letterGrade = 'B';
        } else {
            letterGrade = 'A';
        } // end if statement

        switch (letterGrade) {

            case 'A':
                System.out.println(studentName + " gets an A.");
            case 'B':
                System.out.println(studentName + " gets an B.");
            case 'C':
                System.out.println(studentName + " gets an C.");
            case 'D':
                System.out.println(studentName + " gets an D.");
            case 'F':
                System.out.println(studentName + " gets an F.");

        } // end switch
    } // end method outputGrades
} // end class Student 

感谢您抽空看看!

Thanks for taking a look!

推荐答案

您忘了 switch语句。如果不这样做,它只要继续执行一切,直到块结束!

You forgot the break statements from your switch. If you don't break, it'll just keep executing everything until the end of the block!

这有点混乱的功能是从C遗留下来的,而且在大多数情况下,你要结束所有的情况 s的一个突破收益

This somewhat confusing "feature" is a leftover from C, and in most cases you'll want to end all your cases with a break, return or throw.

这篇关于用于分配寻求援助,以平均品位数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:21