构造函数和方法不提供输出

时间:2014-10-05 08:01:37

标签: java methods constructor

我正在编写关于健康记录计算机化和制作程序的代码。但是当我运行它时,它只给出输出: -

该人的总年数为21613

BMI值 重量不足:小于18.5  正常:介于18.5和24.9之间 超重:25到29.9之间 肥胖:30或更高 输入高度和重量值

但是我的所有其他方法都没有运行和构造函数,请告诉我如何解决这个问题? 我的计划是: -

import java.util.Scanner;

class Starter{

    private String FirstName;
    private String LastName;
    private String Gender;
    private int DOB;
    private int MOB;
    private int YOB;
    private double Height;  //inches
    private double Weight;  //KG.
    String greeting=FirstName+LastName+Gender;
    double BMI1;    
    public Starter(){
        Scanner input=new Scanner(System.in);
        System.out.println("Enter all the required data");
        FirstName=input.nextLine();
        LastName=input.nextLine();
        Gender=input.nextLine();
        DOB=input.nextInt();
        MOB=input.nextInt();
        YOB=input.nextInt();
    }





     public void setFirstName(String Fname){
           FirstName=Fname;

          }
       public void setLastName(String Lname){
           LastName=Lname;
       }
       public void setGender(String G){
           Gender=G;
           }
       public void setBirth(int Day,int Month,int Year){
           DOB=Day;
           MOB=Month;
           YOB=Year;


       }
       public String getFirstName(){
           return FirstName;
       }
       public String getLastName(){
           return LastName;
       }
       public String getGender(){
           return Gender;
       }
       public int getDOB()
       {
        return DOB;
    }
       public int getMOB(){
           return MOB;

       }
       public int getYOB(){
           return YOB;
        }
       public void BMI1(){
           System.out.println("Enter the value of Height and Weight");
           Scanner input1=new Scanner(System.in);
           Height=input1.nextFloat();
           Weight=input1.nextFloat();
            BMI1=Weight/(Height*Height);
            System.out.println("The BMI value is :\t"+BMI1);
       }
      public  void getYears(){
          int TotalYear=2014-YOB;
          int TotalMonth=12-MOB;
          int TotalDay=31-DOB;
          System.out.printf("The total years of that person is %d-%d-%d",TotalYear,TotalMonth,TotalDay);
          System.out.println("\n");
       }
      public    void BMI(){
            System.out.println("BMI values");
            System.out.println("Under weight:\tless than 18.5\n normal:\tbetween 18.5 and 24.9\nOverweight:\tbetween 25 and 29.9\nObese:\t30 or greater");
        }      
}





public class HeartRates {

public static void main(String[] args) {

       Starter getvalues2=new Starter();
       getvalues2.setFirstName("Sachin");
       getvalues2.setLastName("Godara");
       getvalues2.setGender("Male");
       getvalues2.setBirth(18,6,1993);
       getvalues2.getFirstName();
       getvalues2.getLastName();
       getvalues2.getGender();
       getvalues2.getDOB();
       getvalues2.getMOB();
       getvalues2.getYOB();
       getvalues2.getYears();
       getvalues2.BMI();
       getvalues2.BMI1();




        }


}

我的输出应该是: -

Enter all the required data
sam 
godara
male
18
6
1993
The total years of that person is 21-6-13

BMI values
Under weight:   less than 18.5
 normal:    between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese:  30 or greater
Enter the value of Height and Weight
 2
78
The BMI value is :  19.5
Sachin 
godara
male
1861993

2 个答案:

答案 0 :(得分:2)

您缺少构造函数。如果这样:

void starter(){
    Scanner input=new Scanner(System.in);
    System.out.println("Enter all the required data");
    FirstName=input.nextLine();
    LastName=input.nextLine();
    Gender=input.nextLine();
    DOB=input.nextInt();
    MOB=input.nextInt();
    YOB=input.nextInt();
}

意味着构造函数然后是错误的。尽管构造函数是一个void方法,但它并没有像那样声明。将其更改为:

public Starter(){
    Scanner input=new Scanner(System.in);
    System.out.println("Enter all the required data");
    FirstName=input.nextLine();
    LastName=input.nextLine();
    Gender=input.nextLine();
    DOB=input.nextInt();
    MOB=input.nextInt();
    YOB=input.nextInt();
}

更新1

这些方法:

getvalues2.getFirstName();
getvalues2.getLastName();
getvalues2.getGender();
getvalues2.getDOB();
getvalues2.getMOB();
getvalues2.getYOB();
getvalues2.getYears();

返回一个值。这并不意味着他们必须打印它。

尝试

system.out.println(getvalues2.getFirstName());
system.out.println(getvalues2.getLastName());
....//etc

更新2

  

也为什么给予该人的总年数为21613英镑   输出

这是因为:

System.out.printf("The total years of that person is %d%d%d",TotalYear,TotalMonth,TotalDay);

%d%d%d表示printf期望3 ints作为争论,然后彼此相邻。

我认为这对你更有意义:

`%d%d%d` output `123`
`%d %d %d` output `1 2 3`
`%d-%d-%d` output `1-2-3`

所以你可以适当地改变它。

更新3

 public void BMI1(){
     System.out.println("Enter the value of Height and Weight");
     Scanner input1=new Scanner(System.in);
     Height=input1.nextFloat();
     Weight=input1.nextFloat();
     BMI1=Weight/(Height*Height);
     System.out.println("The BMI value is :\t"+BMI1);
 }

从你的评论我假设你的porgramm挂在这里。此方法期望您在打印出某些内容之前输入2 float s。因此,如果您输入的内容未能获得System.out.println("The BMI value is :\t"+BMI1);

答案 1 :(得分:1)

如果这个方法应该是构造函数:

void starter(){

应该是:

public Starter(){

否则,它只是一个常规类方法,并且在您调用new Starter()时不会执行。

构造函数没有返回类型,Java区分大小写。

编辑:

更改:

System.out.printf("The total years of that person is %d%d%d",TotalYear,TotalMonth,TotalDay);

到:

System.out.printf("The total years of that person is %d %d %d",TotalYear,TotalMonth,TotalDay);

为了使输出更具可读性。