Java - 心率计算器帮助inCode

时间:2016-09-11 16:53:36

标签: java

这是我的代码:

import java.util.Scanner;

public class Pulse {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter your first name: ");
        String firstName = input.nextLine();

        System.out.print("Enter your age: ");
        int age = input.nextInt();

        Pulse person1 = new Pulse();

        System.out.printf("Calculations for %s %s age %d\n",
                person1.getFirstName(), person1.getAge());

        System.out.printf("Maximum Heart Rate: %d BPM (beats per minute)\n", person1.getMaxHeartRate());

        System.out.print("Target Heart Rate Range is: ");
        person1.displayTargetHeartRateRange();
        System.out.println();
    }

    private void displayTargetHeartRateRange() {
        // TODO Auto-generated method stub

    }

    private Object getMaxHeartRate() {
        // TODO Auto-generated method stub
        return null;
    }

    private Object getAge() {
        // TODO Auto-generated method stub
        return null;
    }

    private Object getFirstName() {
        // TODO Auto-generated method stub
        return null;
    }

}

我遇到了getAge和所有GET方法的问题。我创建了一个私有对象来修复错误消息,但现在它们没有正确执行。有人请帮帮我

1 个答案:

答案 0 :(得分:-1)

System.out.printf( "Calculations for %s %s age %d\n", 
 person1.getFirstName(), person1.getAge() );

这里调用函数getFirstName并将返回值赋给%s,但返回值为null,因为你在这里设置了值:

private Object getFirstName() {
    // TODO Auto-generated method stub
    return null;
}

从技术上讲,这是一个双重错误,因为您还使用“Object”作为返回类型。编译器将推断出要返回的正确类型,但是返回null可能导致编译器出错。

适当的代码:

import java.util.Scanner;

public class Pulse
{
    public static void main( String[] args )
    {
        Scanner input = new Scanner( System.in );
        Pulse person1 = new Pulse();

        System.out.print( "Enter your first name: ");
        person1.firstName = input.nextLine();


        System.out.print( "Enter your age: ");
        person1.age = input.nextInt();

        System.out.printf( "Calculations for %s age %d\n", 
            person1.getFirstName(), person1.getAge() );

        System.out.printf( "Maximum Heart Rate: %d BPM (beats per minute)\n", person1.getMaxHeartRate() ); // Here, you haven't set the maxHeartRate yet, so it will be 0.

        //System.out.print( "Target Heart Rate Range is: ");
        //person1.displayTargetHeartRateRange();
        //System.out.println();

        input.close();
    }

    private int age, maxHeartRate;
    private String firstName;

    public void displayTargetHeartRateRange() {
        // TODO Auto-generated method stub
        // Well here you don't have anything to show because you haven't create the range of heart rate.
        // You can create an array recording all the heart rate, and sort out the highest and the lowest heart rate.
    }

    public Object getMaxHeartRate() {
        // TODO Auto-generated method stub
        return maxHeartRate;
    }

    public Object getAge() {
        // TODO Auto-generated method stub
        return age;
    }

    public Object getFirstName() {
        // TODO Auto-generated method stub
        return firstName;
    }
}

运行此代码,您应该得到您期望的结果。