【问题标题】:Adding multiple test scores to student class向学生班级添加多个考试成绩
【发布时间】:2018-10-04 12:04:00
【问题描述】:

将教材第7章中的Student类修改如下: 每个学生对象还应包含三个不同测试的分数。在现有构造函数中将所有测试分数初始化为零。创建第二个构造函数(或重载构造函数)以根据参数参数值设置所有实例值。 提供此方法:

setTestScore:接受两个参数,测试编号(1 到 3)和分数。

getTestScore:接受测试号并返回相应的分数。

average:计算并返回学生的平均考试成绩。

toString: 考试成绩和平均成绩包含在学生的描述中。

之后修改驱动类的main方法来演示新的Student方法。

我卡在它(提示?)接受测试编号(1-3)的部分,然后根据该数字选择测试分数。但是,如果直接来自班级,我认为我不能输入任何整数。

package StudentBody;

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

    Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);
    Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);

    Student john = new Student("John", "Smith", jHome, school);

    Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
    Student marsha = new Student("Marsha", "Jones", mHome, school);

    System.out.println(john);
    System.out.println();
    System.out.println(marsha);

    }
}

public class Student {

private String firstName, lastName;
private Address homeAddress, schoolAddress;
private double score1, score2, score3;
private int testnumber;

public Student(String first, String last, Address home, Address school)
{
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;
    score1 = 0;
    score2 = 0;
    score3 = 0;
    testnumber = 0;
}
public Student(double score1_, double score2_, double score3_, int testnumber_)
{
    score1 = score1_;
    score2 = score2_;
    score3 = score3_;
    testnumber = testnumber_;
}
public void setTestScore(double score1_1, double score2_1, double score3_1, int testnumber1_1)
{
    score1 = score1_1;
    score2 = score2_1;
    score3 = score3_1;
    testnumber = testnumber1_1;
}
public double getTestScore()
{

}
public String toString()
{
    String result;

    result = firstName + " " + lastName + "\n";
    result += "Home Address:\n" + homeAddress + "\n";
    result += "School Address:" + schoolAddress;

    return result;
    }
}


public class Address {  
private String streetAddress, city, state;
private long zipCode;

public Address(String street, String town, String st, long zip)
{
    streetAddress = street;
    city = town;
    state = st;
    zipCode = zip;
}
public String toString()
{
    String result;

    result = streetAddress + "\n";
    result += city + ", " + state + " " + zipCode;

    return result;
}

}

【问题讨论】:

  • 输入为逗号分隔值,例如10,12,5 然后使用 String::split 获取每个单独的值
  • 我认为使用像 score = new int[3] 这样的数组来保存分数并只获取您在方法描述中提到的一个数字并返回数组的相关部分或设置相关部分。 (因为方法必须只有两个参数,测试号和分数)我认为你做错了,因为你接受三个数字
  • 注意:您不应该在 Java 的标识符中使用下划线。 Java 命名约定不鼓励它。此外,包名称不应包含大写字符。
  • 最简单的方法是你可以使用 HashMap 和 has put 和 get 方法 put 有两个参数第一个是 Integer,你可以调用测试号,第二个是 Double,即分数。要获得分数,您可以使用 get 将参数作为整数,即您的测试编号。要查找平均值,您可以使用 Map.entrySet().iterator() 并查找平均值。

标签: java


【解决方案1】:

我看到您已将分数和测试编号用作变量,即如下所示

private double score1, score2, score3;
private int testnumber;

相反,尝试将其作为 hashMap 。更改您的代码

    public void setTestScore(double score1_1, double score2_1, double score3_1, int testnumber1_1)
{
    score1 = score1_1;
    score2 = score2_1;
    score3 = score3_1;
    testnumber = testnumber1_1;
}

到下面的代码:

private HashMap<Integer, Integer> score;
    public void setTestScore( int testnumber,double score)
{
    score.put(testnumber,score);
}

另外,在初始构造函数中,将 hashMap 值设置为零。

    testMarks.put(1, 0);
    testMarks.put(2, 0);
    testMarks.put(3, 0);//1,2,3 are the test numbers

另外,这里不需要 2 个构造函数,因为重载的构造函数和 setTestScore 看起来一样。因此,使用初始构造函数并从代码中删除重载的构造函数。

【讨论】:

    【解决方案2】:

    我认为您的实现与您在第一个问题中的描述不正确。我修改了您的代码以了解如何执行此操作:
    更新为不使用数组

    public class StudentBody
    {
        public static void main(String[] args) {
            Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085);
            Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551);
            Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
    
            Student john = new Student("John", "Smith", jHome, school);
            Student marsha = new Student("Marsha", "Jones", mHome, school);
    
            Scanner in = new Scanner(System.in);
            int testNumber = in.nextInt(); // ask for test number
    
            System.out.println(john.getTestScore(testNumber));
            System.out.println(marsha.getTestScore(testNumber));
    
            // rest of your code
        }
    }
    
    class Student
    {
        private String firstName, lastName;
        private Address homeAddress, schoolAddress;
        private double score1, score2, score3;
    
        public Student(String firstName, String lastName, Address homeAddress, Address schoolAddress) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.homeAddress = homeAddress;
            this.schoolAddress = schoolAddress;
            // there is no need for score1 = score2 = score3 = 0; it is their default value
            // when object created.
        }
    
        public void setTestScore(int testNumber, double newScore) {
            if (testNumber < 1 || testNumber > 3) {
                // do something because test number is wrong
            }
            if (testNumber == 1)
                score1 = newScore;
            else if (testNumber == 2)
                score2 = newScore;
            else
                score3 = newScore;
        }
    
        public double getTestScore(int testNumber) {
            if (testNumber < 1 || testNumber > 3) {
                // do something because test number is wrong
            }
            if (testNumber == 1)
                return score1;
            else if (testNumber == 2)
                return score2;
            else
                return score3;
        }
    
        public double average() {
            return (score1 + score2 + score3) / 3;
        }
    
        public String toString() {
            return "Scores: " + score1 + " , " + score2 + " , " + score3 +
                    " " + " average: " + average();
        }
    }
    
    class Address
    {
        // same as you
    }
    

    很抱歉代码太多。我希望这是你想要的。

    【讨论】:

    • 我还没用数组,这个项目不用数组也可以搞定
    • @Markusovic 好的。我正在编辑它,所以它不再使用数组。我希望这能解决问题。
    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多