gpt4 book ai didi

java - 构造函数错误(JAVA)

转载 作者:行者123 更新时间:2023-11-29 09:52:59 25 4
gpt4 key购买 nike

所以我能够让我的第一个构造函数工作,但不是我的第二个用于 p2 的构造函数。对于 p2,我试图显示(字符串、字符串、字符串、 double 、 double 、字符串、字符串)。就目前而言,代码显示错误“字符串无法转换为 double ”,但我看不出我在做什么。

这是我的主要代码。 打包学生健康服务;

    public class Studenthealthservices {

public static void main(String[] args) {

EMR p1 = new EMR("Colin","10-22-74");
System.out.printf(p1.toString());

EMR p2 = new EMR("Anquan","9-30-77", "stomach ache","98", "120", "stress", "Tylenol");
System.out.printf(p2.toString());

}}

这是我的 EMR 类(class)的代码 打包学生健康服务;

public class EMR {

private String name;
private String dob;
private String rfv;
private double bodyt;
private double hr;
private String diag;
private String pmeds;

public void setName(String name) {
this.name = name;
}
public EMR (String name, String dob) {
this.name = name;
this.dob = dob;
}
public String getName(){
return name;
}

public EMR (String name, String dob, String rfv, double bodyt, double hr, String diag, String pmeds){
this.name = name;
this.dob = dob;
this.rfv = rfv;
this.bodyt = bodyt;
this.hr = hr;
this.diag = diag;
this.pmeds = pmeds;

}
public String getDob() {
return dob;
}

public void setDob(String dob) {
this.dob = dob;
}

public String getRfv() {
return rfv;
}

public void setRfv(String rfv) {
this.rfv = rfv;
}

public double getBodyt() {
return bodyt;
}

public void setBodyt(double bodyt) {
this.bodyt = bodyt;
}

public double getHr() {
return hr;
}

public void setHr(double hr) {
this.hr = hr;
}

public String getDiag() {
return diag;
}

public void setDiag(String diag) {
this.diag = diag;
}

public String getPmeds() {
return pmeds;
}

public void setPmeds(String pmeds) {
this.pmeds = pmeds;
}

@Override
public String toString(){
return"name :" + this.name +"Date of Birth:" + this.dob;
}
}

最佳答案

"98" 是一个 String,而不是一个 double。您需要删除引号:

 EMR p2 = new EMR("Anquan","9-30-77", "stomach ache", 98, 120, "stress", "Tylenol");

关于java - 构造函数错误(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29950684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com