java - 构造函数和方法没有给出输出

标签 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

最佳答案

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

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

also why it is giving The total years of that person is 21613 in output

发生这种情况是因为:

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

%d%d%d 表示 printf 需要 3 个 int 作为参数,并将它们放置在彼此旁边。

我认为这对您来说更有意义:

`%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);
 }

根据你的评论,我假设你的程序卡在这里。此方法要求您在打印内容之前输入 2 float。因此,如果您不输入某些内容,则不会获得 System.out.println("The BMI value is :\t"+BMI1);

关于java - 构造函数和方法没有给出输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26200763/

相关文章:

java - 如何在接口(interface)实例中调用super方法?

c++在类方法中使用cout

javascript - JS 构造函数中 "this"的值返回 "undefined"

java链表复制构造函数和字符串构造函数

java - Spring Batch DataSourceTransactionManager 在 Oracle 上失败

java - Play Framework 配置

java - PostgreSQL 列中映射的 Java LocalDate/LocalDateTime 字段的问题

objective-c - 带有可变参数后跟非可变参数的方法

c++ - 在编译器生成的复制构造函数上强制模板化构造函数

java - 如何高效地检查数组? ( java )