Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将多个测试分数添加到学生课堂_Java - Fatal编程技术网

Java 将多个测试分数添加到学生课堂

Java 将多个测试分数添加到学生课堂,java,Java,修改教科书第7章中的学生课堂,如下所示: 每个学生对象还应包含三个不同测试的分数。在现有构造函数中将所有测试分数初始化为零。创建第二个构造函数(或重载构造函数)以基于参数值设置所有实例值。 提供以下方法: setTestScore:接受两个参数,测试编号(1到3)和分数 getTestScore:接受测试编号并返回相应的分数 average:计算并返回学生的平均考试分数 toString:考试分数和平均分包含在学生描述中 然后修改driver类的main方法来演示新的Student方法 我被卡在

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

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

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

average
:计算并返回学生的平均考试分数

toString
:考试分数和平均分包含在学生描述中

然后修改driver类的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;
}

}

我认为您的实现与您在第一个问题中的描述不一致。我修改了您的代码以了解如何做到这一点:
更新为不使用数组)

公共班级学生团体
{
公共静态void main(字符串[]args){
地址学校=新地址(“宾夕法尼亚州维拉诺瓦市兰开斯特大道800号”,邮编19085);
地址jHome=新地址(“跳街21号”,“布莱克斯堡”,“弗吉尼亚州”,24551);
地址mHome=新地址(“123大街”、“欧几里得”、“俄亥俄州”44132);
学生约翰=新学生(“约翰”,“史密斯”,约翰,学校);
学生玛莎=新生(“玛莎”,“琼斯”,mHome,学校);
扫描仪输入=新扫描仪(系统输入);
int testNumber=in.nextInt();//询问测试编号
System.out.println(john.getTestScore(testNumber));
System.out.println(marsha.getTestScore(testNumber));
//代码的其余部分
}
}
班级学生
{
私有字符串firstName,lastName;
私人地址、家庭地址、学校地址;
二等兵1分、2分、3分;
公立学生(字符串名、字符串名、地址家庭地址、地址学校地址){
this.firstName=firstName;
this.lastName=lastName;
this.homeAddress=homeAddress;
this.schoolAddress=学校地址;
//不需要score1=score2=score3=0;这是它们的默认值
//创建对象时。
}
公共void setTestScore(int testNumber,双新闻核心){
如果(测试编号<1 | |测试编号>3){
//做点什么,因为测试号码错了
}
if(testNumber==1)
score1=新闻核心;
else if(testNumber==2)
score2=新闻核心;
其他的
score3=新闻核心;
}
公共双getTestScore(int testNumber){
如果(测试编号<1 | |测试编号>3){
//做点什么,因为测试号码错了
}
if(testNumber==1)
返回分数1;
else if(testNumber==2)
返回得分2分;
其他的
回归得分3分;
}
公众双倍平均{
返回(分数1+2+3)/3;
}
公共字符串toString(){
返回“分数:“+score1+”、“+score2+”、“+score3+
“+”平均值:“+average();
}
}
班级地址
{
//和你一样
}

对不起,代码太多了。我希望这就是您想要的。

我看到您使用分数和考试编号作为变量,即如下所示

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);
}

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

以逗号分隔的值输入,例如,
10,12,5
,然后使用String::split获取每个单独的值我想使用类似score=new int[3]的数组保存分数并仅获取方法描述中提到的一个数字,然后返回数组的相关部分或集合相关部分。(因为方法必须只获得两个参数,testnumber和score)我不允许您做错事,因为您接受了三个数字注意:在Java中,您不应该在标识符中使用下划线。Java命名约定不鼓励这样做。此外,包名称不应包含大写字符。最简单的方法是,您可以将HashMap与has put和get方法一起使用,put有两个参数,第一个是整数,您可以调用测试编号,第二个是Double,即score。为了得到分数,你可以使用get,它的参数是整数,这是你的测试编号。要查找平均值,可以使用Map.entrySet().iterator()和find average。我还没有使用数组,这个项目可以不用arrays@Markusovic好啊我正在编辑它,这样它就不再使用数组了。我希望这能解决问题。
private HashMap<Integer, Integer> score;
    public void setTestScore( int testnumber,double score)
{
    score.put(testnumber,score);
}
    testMarks.put(1, 0);
    testMarks.put(2, 0);
    testMarks.put(3, 0);//1,2,3 are the test numbers