初学者Java问题。我有一个任务,需要创建一个新类,在这个类中有一个computeRatio()方法,它将变量低密度脂蛋白除以高密度脂蛋白并打印出比率。我似乎不能把计算结果打印出来。我将发布我得到的代码和输出。我很感谢你能给我的任何指导。
‘公共类SimonChristopher_Checkup {
private int patient;
private int systolic;
private int diastolic;
private int ldl;
private int hdl;
public static void computeRatio(int ldl, int hdl) { // Divides LDL by HDL
// to return ratio
double ratio = ldl/hdl;
System.out.println("The ratio of good and bad cholesterol is " + ratio + ".");
}
public SimonChristopher_Checkup(int p, int s, int d, int l, int h) { // constructor
patient = p;
systolic = s;
diastolic = d;
ldl = l;
hdl = h;
}
public int getPatient() { // accessor (getters)
return patient;
}
public void setPatient(int p) { // mutator (setter)
if (p > 0 && p <= 100) {
patient = p;
}
}
public int getSystolic() { // accessor (getters)
return systolic;
}
public void setSystolic(int s) { // mutator (setter)
if (s >= 100 && s <= 150) {
systolic = s;
}
}
public int getDiastolic() { // accessor (getters)
return diastolic;
}
public void setDiastolic(int d) { // mutator (setter)
if (d >= 50 && d <= 120) {
diastolic = d;
}
}
public int getLdl() { // accessor (getters)
return ldl;
}
public void setLdl(int l) { // mutator (setter)
{
ldl = l;
}
}
public int getHdl() { // accessor (getters)
return hdl;
}
public void setHdl(int h) { // mutator (setter)
{
hdl = h;
}
}
public String toString() { // mandatory convention: returns a String object
// representation
String result;
result = "Patient number: " + patient + "\n" + "Systolic: " + systolic + "\n" + "Diastolic: " + diastolic + "\n"
+ "LDL: " + ldl + "\n" + "HDL: " + hdl + "\n"
+ "HDL is known as good chloresterol. A ratio of 3.5 or better is considered optimal.";
return result;
}}`公共类Simon_TestCheckup {
public static void main(String[] args) {
SimonChristopher_Checkup patient1 = new SimonChristopher_Checkup(223, 100, 80, 105, 60);
SimonChristopher_Checkup patient2 = new SimonChristopher_Checkup(244, 102, 76, 101, 62);
System.out.println(patient1);
System.out.println("--------------------------------------------------------");
System.out.println(patient2);
}}当我运行TestCheckup时,我得到以下输出:
病例数: 223收缩期: 100舒张期: 80低密度脂蛋白胆固醇: 105高密度脂蛋白胆固醇: 60
高密度脂蛋白被称为有益的氯固醇。3.5或更高的比率被认为是最优的。
患者编号: 244收缩期: 102舒张期: 76低密度脂蛋白: 101高密度脂蛋白: 62高密度脂蛋白被称为良好的胆固醇。3.5或更高的比率被认为是最优的。
发布于 2017-04-25 01:17:58
几件事,
首先,看起来您在任何时候都不会调用computeRatio(),因此它永远不会打印任何东西。
其次,我会将其更改为
public double computeRatio(int ldl, int hdl) { // Divides LDL by HDL
return ldl/hdl;
}并在您的toString()方法中添加:
public String toString() { // mandatory convention: returns a String object
// representation
String result;
result = "Patient number: " + patient + "\n" + "Systolic: " + systolic + "\n" + "Diastolic: " + diastolic + "\n"
+ "LDL: " + ldl + "\n" + "HDL: " + hdl + "\n"
+ "The ratio of good and bad cholesterol is " + computeRatio(ldl,hdl) + ".");
+ "HDL is known as good chloresterol. A ratio of 3.5 or better is considered optimal.";
return result;
}此外,您可能希望将hdl和ldl声明为双倍。当你得到ratio时,你希望得到一个双精度,但你是除以2个整数。它现在应该可以像预期的那样工作,但在未来你可能会遇到问题。
https://stackoverflow.com/questions/43589289
复制相似问题