[英]How do i do math with object instance variables once i have created the object in java
I have constructed an object in java with integer instance variables, how do find the quotient of two of these variables after i have created the object.我用整数实例变量在java中构造了一个对象,在创建对象后如何找到其中两个变量的商。
The objects i have created are like this:我创建的对象是这样的:
class Student(int score, int marks)
where score is the score he received in the test and marks is the max score in the test, how can i find the percentage he got, after i have created multiple students like:其中 score 是他在测试中获得的分数,而 mark 是测试中的最高分数,在我创建了多个学生之后,我如何才能找到他得到的百分比:
Student student1 = new Student(60, 90);
where i want the program to print:我希望程序在哪里打印:
"student1 got "+((score/marks)*100)+"%"
Thanks guys!多谢你们!
Here's what I'd recommend:这是我的建议:
class Student {
private int grade;
private int maxGrade;
public Student(int grade, int maxGrade) {
this.grade = grade;
this.maxGrade = maxGrade;
}
public double getPercentage() {
return 100.0 * grade/maxGrade;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.