gpt4 book ai didi

java - 使用 Scanner 类获取构造函数变量的用户输入

转载 作者:行者123 更新时间:2023-12-02 01:19:35 25 4
gpt4 key购买 nike

我正在尝试使用 Scanner 对象接收用户输入,然后将其归因于名为 TargetHeartRateCalculator 的对象的以下值。

TargetHeartRateCalculator 采用构造函数参数..

字符串 fName、字符串 lName、int 月、int 日、int 年。

通常,在不使用扫描仪的情况下,我只会使用参数手动实例化对象,例如...

TargetHeartRateCalculator patient1 = new TargetHeartRateCalculator("Tom", "Willickers", 01, 25, 1966);

我需要使用扫描仪获取用户输入,然后从用户收到的输入中将值分配给 fName、lName、月、日、年。

我尝试通过 Scanner 对象将对象实例化作为用户输入分配的一部分,其语法不正确,我什至不确定您是否应该这样做。

我觉得这可能是一个简单的解决方案,但答案对我来说相当回避。

这是我的驱动程序类...


import java.util.Scanner;

public class DriverClass {



//--------------------------------------------------------------------------------------
// TARGET HEART RATE CALCULATOR CLASS FUNCTIONALITY TEST
//--------------------------------------------------------------------------------------

TargetHeartRateCalculator patient1 = new TargetHeartRateCalculator("Tom", "Willickers", 01, 25, 1966);
TargetHeartRateCalculator patient2 = new TargetHeartRateCalculator("Bill", "Skarsgard", 8,9, 1990);



//Write a java app that prompts for the persons information
//instantiates an object and prints the information from that object
//first name, last name, date of birth,
// calculates maximum heart rate, and target heart rate.
//and then displays them to the them.

Scanner input = new Scanner(System.in);

System.out.print("Please enter your first name: ");
String fnInput = input.nextDouble();
System.out.printf("%nThank you %s %s. Your date of birth is %n" +
"%d and you are %d years old!%n" +
"Your maximum heart rate is %.2f and your %n" +
"Target Heart Rate range is %.2f%n%n" get,



patientInfo(patient1);
patientInfo(patient2);

displayAgeYears(patient1);
displayAgeYears(patient2);

displayMaxHeartRate(patient1);
displayMaxHeartRate(patient2);

displayTargetHeartRate(patient1);
displayTargetHeartRate(patient2);


}
}

这是我的类(class)


import java.time.LocalDateTime;

public class TargetHeartRateCalculator {
private String fName;
private String lName;
private int dOB;
private int bMonth;
private int bDay;
private int bYear;
private int ageYears;
private double maxHeartRate;
private double lowTargetHearRate;
private double highTargetHearRate;

LocalDateTime now = LocalDateTime.now();
int cYear = now.getYear();
int cMonth = now.getMonthValue();
int cDay = now.getDayOfMonth();

//constructor
public TargetHeartRateCalculator(String fNameIn, String lNameIn, int monthIn, int dayIn, int yearIn) {
fName = fNameIn;
lName = lNameIn;
bMonth = monthIn;
bDay = dayIn;
bYear = yearIn;
}

public String getfName() {
return fName;
}

public void setfName(String fName) {
this.fName = fName;
}

public String getlName() {
return lName;
}

public void setlName(String lName) {
this.lName = lName;
}

public int getBMonth() {
return bMonth;
}

public void setBMonth(int month) {
this.bMonth = month;
}

public int getBDay() {
return bDay;
}

public void setBDay(int day) {
this.bDay = day;
}

public int getBYear() {
return bYear;
}

public void setBYear(int year) {
this.bYear = year;
}

public int getAgeYear(){
int currentAgeYear = cYear -bYear;
return currentAgeYear;
}

public int getAgeMonth(){
int currentAgeMonth =cMonth - bMonth;
return currentAgeMonth;
}

public int getAgeDay(){
int currentAgeDay =cDay - bDay;
return currentAgeDay;
}



public String getdOB(TargetHeartRateCalculator patient) {

String dOB = String.format("%s/%s/%s",
patient.getBMonth(), patient.getBDay(), patient.getBYear());

return dOB;
}

public void setdOB(int dOB) {
this.dOB = dOB;
}

public static String displayAgeYears(TargetHeartRateCalculator patient) {

String ageYears = String.format("%s %s is %s Years old",
patient. getfName(), patient.getlName(), patient.getAgeYear());

return ageYears;
}

public void setAgeYears(int ageYears) {

this.ageYears = ageYears;
}

public double getMaxHeartRate() {
double maxHeartRate = (220 - getAgeYear()) ;

return maxHeartRate;
}

public void setMaxHeartRate(int maxHeartRate) {
this.maxHeartRate = maxHeartRate;
}

public double getLowTargetHearRate() {
lowTargetHearRate = getMaxHeartRate() * .5;

return lowTargetHearRate;
}

public void setLowTargetHearRate(int lowTargetHearRate) {
this.lowTargetHearRate = lowTargetHearRate;
}

public double getHighTargetHeartRate(){
highTargetHearRate = getMaxHeartRate() * .85;

return highTargetHearRate;
}

public void setHighTargetHearRate(){
this.highTargetHearRate = highTargetHearRate;
}

public static String displayTargetHeartRate(TargetHeartRateCalculator patient){
String hRateRange = String.format("%.2f BPM - %.2f BPM", patient.getLowTargetHearRate(), patient.getHighTargetHeartRate());

return hRateRange;
}

public static String displayMaxHeartRate(TargetHeartRateCalculator patient){
String mHeartRate = String.format("%.2f BPM", patient.getMaxHeartRate());

return mHeartRate;
}




public static String patientInfo(TargetHeartRateCalculator patient) {

String result = String.format("Patient Name: %s %s DOB: %d/%d/%d",
patient.getfName(), patient.getlName(), patient.getBMonth(), patient.getBDay(), patient.getBYear());

return result;


}
}

最佳答案

您应该阅读构建 TargetHeartRateCalculator 实例所需的所有变量。

System.out.print("Please enter your first name: ");
String firstName = input.next();
System.out.print("Please enter your lastname: ");
String lastName = input.next();
System.out.print("Please enter your birthday day: ");
String birthdayDay = input.nextInt();
System.out.print("Please enter your birthday month: ");
String birthdayMonth = input.nextInt();
System.out.print("Please enter your birthday year: ");
String birthdayYear = input.nextInt();

TargetHeartRateCalculator patient = TargetHeartRateCalculator(firstName, lastName,
birthdayDay, birthdayMonth, birthdayYear);

然后,您可以在初始化 TargetHeartRateCalculator 后调用静态方法。

TargetHeartRateCalculator.displayTargetHeartRate(patient);

但是,您不应将所有内容放入类中并在 TargetHeartRateCalculator 中使用静态方法,而应将 TargetHeartRateCalculator 分为两个,其中可以是 PatientTargetHeartRateCalculator

患者类别可以是这样的:

import java.time.LocalDateTime;

public class Patient {

private String fName;
private String lName;
private int dOB;
private int bMonth;
private int bDay;
private int bYear;
private int ageYears;

private int currentYear = LocalDateTime.now().getYear();
private int currentMonth = LocalDateTime.now().getMonthValue();
private int currentDay = LocalDateTime.now().getDayOfMonth();

public Patient(String fName, String lName, int bMonth, int bDay, int bYear) {
this.fName = fName;
this.lName = lName;
this.bMonth = bMonth;
this.bDay = bDay;
this.bYear = bYear;
}

public String getfName() {
return fName;
}

public void setfName(String fName) {
this.fName = fName;
}

public String getlName() {
return lName;
}

public void setlName(String lName) {
this.lName = lName;
}

public int getbMonth() {
return bMonth;
}

public void setbMonth(int bMonth) {
this.bMonth = bMonth;
}

public int getbDay() {
return bDay;
}

public void setbDay(int bDay) {
this.bDay = bDay;
}

public int getbYear() {
return bYear;
}

public void setbYear(int bYear) {
this.bYear = bYear;
}

public int getAgeYear(){
return currentYear -bYear;
}

public int getAgeMonth(){
return currentMonth - bMonth;
}

public int getAgeDay(){
return currentDay - bDay;
}

}

计算将在另一个类中进行,该类将 Patient 对象作为参数并进行计算。

关于java - 使用 Scanner 类获取构造函数变量的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950693/

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