使用Scanner类接收用户输入的构造函数变量

时间:2019-09-16 05:10:16

标签: java

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

TargetHeartRateCalculator带有..的构造函数参数。

字符串fName,字符串lName,整数月,整数日,整数年。

通常不使用扫描仪,我只是使用参数手动实例化对象,例如...

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);


    }
}

这是我的班级


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;


    }
}

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您正在尝试获取构造函数的输入吗?我将显示一个字段,因为其余字段都相同

Scanner input = new Scanner(System.in);

System.out.print("Please enter your first name: ");
String fnInput = input.nextLine();
// repeat print and scan, as needed 

TargetHeartRateCalculator patient1 = TargetHeartRateCalculator(fnInput);  // add more parameters 

然后您将要拥有 instance方法,而不是具有参数的静态方法。

public void patientInfo() {
    System.out.printf("%s%n", this.getfName()); // for example 
} 

您这样调用这些方法

patient1.patientInfo();

我认为您应该从main方法中删除打印语句

答案 1 :(得分:1)

您应该阅读构造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。 / p>

患者类可以是这样的:

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对象作为参数并进行计算。