在我的代码中,我从数组中得到一个0-10之间的“面试分数”输入。我应该映射0到0和10到100,所以基本上把面试分数乘以10。
对象的构造函数是
public Person(String firstName, String lastName, double interview) {
this.firstName = firstName;
this.lastName = lastName;
this.gpa = gpa;
this.interview = interview;
}我的目标是
Person s1 = new Person("name", "surname", 3.5, 8);在这里,8是面试分数,3.5是GPA分数(不一定是我问题的一部分)。
在我使用的get和set方法中
public double getInterview() {
return interview;
}
public void setInterview(double interview) {
this.interview = interview*10;
}期望它乘以10,所以我可以在同一个类中的getTotalPoints方法中使用它,即:
points = getGpa()*gpaWeight + getInterview()*intWeight;但这里的面试分数是8,而不是80。
我能做些什么来解决这个问题?
谢谢
(我真的不知道地图等,所以我不知道它是否会在这里工作,如果以这种格式给出任何答案,我将不胜感激)
发布于 2022-06-04 09:39:35
您正在使用构造函数来设置访谈值,而不是使用setter方法setInterview()。
使用setInterview()方法设置面试值或修改构造函数,如下所示:
public Person(String firstName, String lastName, double interview) {
this.firstName = firstName;
this.lastName = lastName;
this.gpa = gpa;
this.interview = interview * 10;
}发布于 2022-06-04 10:00:11
问题是,在构造函数和setter之间,在“面试”属性的表示形式上存在不一致性。
在构造函数中:
this.interview = interview;在这里,您的工作范围是从0到10。
鉴于在策划人
this.interview = interview*10;在这里,您的工作范围是从0到100。
您可以在0-100标度中设置值并以这种方式使用,也可以将其设置为0-10小数位,每次使用它时,您都必须记住将其映射到0-100标度。我认为最好的解决办法是前者。
关于从一个范围到另一个范围的映射,您可以从以下答案中获得灵感:Mapping a numeric range onto another
发布于 2022-06-04 09:39:10
您永远不会调用setInterview函数。如果您不调用它,那么值将如何变化。假设您只使用constructor传递值。我建议在setInterview内部打电话给constructor。
public Person(String firstName, String lastName, double interview) {
this.firstName = firstName;
this.lastName = lastName;
this.gpa = gpa;
setInterview(interview); // or you can simply assign the value here
}https://stackoverflow.com/questions/72498615
复制相似问题