【发布时间】:2014-11-29 19:11:39
【问题描述】:
我正在编写关于健康记录计算机化的代码并制作程序。但是当我运行它时,它只给出输出:-
那个人的总年数是 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
【问题讨论】:
-
你的命名很混乱,请关注Java Naming Conventions。
-
但是我不明白先生,为什么命名如此重要,至少我的整个程序应该运行。但为什么它没有运行?
-
编译器不关心命名,我们关心。这是为了可读性。
-
那为什么我的构造函数和其他方法没有运行?
-
我更正了构造函数中的错误,但我的其他方法没有运行?
标签: java methods constructor