java初学者,下面程序为什么运行结果有问题,一个类访问不到另一个类的变量??
publicclassCalculation{publicdoubleweight;publicdoubleheight;publicdoubleBMI;publicSt...
public class Calculation {
public double weight;
public double height;
public double BMI;
public String state;
public void calculate(double weight, double height){
this.weight=weight;
this.height=height;
BMI=(weight*1.0*10000/(height)/(height));
BMI=(int)(BMI*10+0.5)/10.0;// round the number
if (BMI<18.5){
state="Underweight";
}
if(BMI>=18.5&&BMI<=24.9){
state="Normal";
}
if (BMI>=25&&BMI<=29.9){
state="Overweight";
}
if(BMI>=29.9){
state="Obese";
}
}
public double getWeight(){
return weight;
}
}
public class Display {
private Calculation cal= new Calculation();
public void display(){
System.out.println("Your weight: "+cal.getWeight()+"kg");
System.out.println("Your height: "+new Calculation().height+"cm");
System.out.println("Your BMI: "+new Calculation().BMI+"kg");
System.out.println("Your are in the "+new Calculation().state+" range");
}
}
public class BMI_tests {
public static void main(String[] args) {
double para1=Double.parseDouble(args[0]);
System.out.println(para1);
double para2=Double.parseDouble(args[1]);
System.out.println(para2);
Calculation ob=new Calculation();
Display ob2=new Display();
ob.calculate(para1,para2);
ob2.display();
}
}
输入72kg和170cm时,结果为:
Your weight: 0.0kg
Your height: 0.0cm
Your BMI: 0.0kg
Your are in the null range。
为什么Display类中,weight等变量没有得到Calculation中这些变量的数值? 展开
public double weight;
public double height;
public double BMI;
public String state;
public void calculate(double weight, double height){
this.weight=weight;
this.height=height;
BMI=(weight*1.0*10000/(height)/(height));
BMI=(int)(BMI*10+0.5)/10.0;// round the number
if (BMI<18.5){
state="Underweight";
}
if(BMI>=18.5&&BMI<=24.9){
state="Normal";
}
if (BMI>=25&&BMI<=29.9){
state="Overweight";
}
if(BMI>=29.9){
state="Obese";
}
}
public double getWeight(){
return weight;
}
}
public class Display {
private Calculation cal= new Calculation();
public void display(){
System.out.println("Your weight: "+cal.getWeight()+"kg");
System.out.println("Your height: "+new Calculation().height+"cm");
System.out.println("Your BMI: "+new Calculation().BMI+"kg");
System.out.println("Your are in the "+new Calculation().state+" range");
}
}
public class BMI_tests {
public static void main(String[] args) {
double para1=Double.parseDouble(args[0]);
System.out.println(para1);
double para2=Double.parseDouble(args[1]);
System.out.println(para2);
Calculation ob=new Calculation();
Display ob2=new Display();
ob.calculate(para1,para2);
ob2.display();
}
}
输入72kg和170cm时,结果为:
Your weight: 0.0kg
Your height: 0.0cm
Your BMI: 0.0kg
Your are in the null range。
为什么Display类中,weight等变量没有得到Calculation中这些变量的数值? 展开
1个回答
展开全部
Display类中定义了一个自己的对象,如下
private Calculation cal= new Calculation();
还有后面三句打印语句的new Calculation(),都是重新定义了变量。
运行是打印的是这些对象变量的值,在Display类中没有给他们赋值,所以打印时,是没有结果的。
修改如下:
public class Display {
private Calculation cal ;
public void display(Calculation calPara ){
cal = calPara ;
System.out.println("Your weight: "+cal.getWeight()+"kg");
System.out.println("Your height: "+cal..height+"cm");
System.out.println("Your BMI: "+cal.BMI+"kg");
System.out.println("Your are in the "+cal..state+" range");
}
}
public class BMI_tests {
public static void main(String[] args) {
double para1=Double.parseDouble(args[0]);
System.out.println(para1);
double para2=Double.parseDouble(args[1]);
System.out.println(para2);
Calculation ob=new Calculation();
Display ob2=new Display();
ob.calculate(para1,para2);
ob2.display(ob);
}
}
private Calculation cal= new Calculation();
还有后面三句打印语句的new Calculation(),都是重新定义了变量。
运行是打印的是这些对象变量的值,在Display类中没有给他们赋值,所以打印时,是没有结果的。
修改如下:
public class Display {
private Calculation cal ;
public void display(Calculation calPara ){
cal = calPara ;
System.out.println("Your weight: "+cal.getWeight()+"kg");
System.out.println("Your height: "+cal..height+"cm");
System.out.println("Your BMI: "+cal.BMI+"kg");
System.out.println("Your are in the "+cal..state+" range");
}
}
public class BMI_tests {
public static void main(String[] args) {
double para1=Double.parseDouble(args[0]);
System.out.println(para1);
double para2=Double.parseDouble(args[1]);
System.out.println(para2);
Calculation ob=new Calculation();
Display ob2=new Display();
ob.calculate(para1,para2);
ob2.display(ob);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询