初学Java问题。我有一个赋值,要求我创建一个新类,在该类中是一个computeRatio()方法,它将变量LDL除以HDL并打印出比率。我似乎无法使计算工作和打印出来。我将发布我得到的输出代码。我感谢您给予的任何指导。
'公共课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 LDL:105 HDL:60
患者编号:244 收缩压:102 舒张期:76 LDL:101 HDL:62 HDL被称为良好的chloresterol。比率为3.5或更高被认为是最佳的。
答案 0 :(得分:0)
Few things,
Firstly, it doesn't look like you call upon computeRatio() at any time thus it will never print anything.
Secondly, I would change it to
public double computeRatio(int ldl, int hdl) { // Divides LDL by HDL
return ldl/hdl;
}
and in your toString() method add:
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;
}
Also, you may want to declare hdl and ldl as doubles down the line. When you get ratio you're looking to get a double but you're dividing 2 ints. It should work as intended for now but in the future you may run into problems.