如何在构造函数中传递参数?

时间:2015-02-21 23:03:31

标签: java jgrasp

我正在做一个简单的Java作业。看起来我的语法和逻辑正确。但是,构造函数无法理解我试图传递的论点,这就是为什么所有计算都是错误的。我附上了输出。任何人都知道如何解决这个问题?任何帮助将不胜感激。谢谢!

public class DT_CarStats
 {
   private double gallons;
   private double mpg;
   private double ppg;

public DT_CarStats(double gallons, double mpg, double ppg)
{
  gallons = gallons;
  mpg = mpg;
  ppg = ppg;
}

public double CostPer100()
{ 
  return 100 / (mpg * ppg);
}

public double MaxDistance()
{
  return mpg * gallons;
}
}
import java.util.Scanner;

public class DT_CarStatsTester
{
  public static void main(String[] args)
  {
    Scanner scannerObject = new Scanner(System.in);
    double gallons, mpg, ppg;
    System.out.printf("******************************************* \n");
    System.out.printf("* Welcome to my Distance to Empty App  * \n");
    System.out.printf("******************************************* \n");
    System.out.printf("Please enter the number of gallons of gas in the     tank: ");
    gallons = scannerObject.nextDouble();
    System.out.printf("Please enter the fuel efficiency (miles per gallon): ");
    mpg = scannerObject.nextDouble();
    System.out.printf("Please enter the price of gas per gallon: ");
    ppg = scannerObject.nextDouble();
    DT_CarStats cs = new DT_CarStats(gallons,mpg,ppg);
    System.out.printf("\n");
    System.out.printf("To drive 100 miles, it will cost $" + cs.CostPer100() + ". \n");
    System.out.printf("The car can currently drive a maximum of " + cs.MaxDistance() + " miles. \n");
    System.out.printf("******************************************* \n");
    System.out.printf("* Thanks for using our App, Safe Travels! * \n");
    System.out.printf("******************************************* \n");
  }
}

示例运行:

******************************************* 
******************************************* 
* Welcome to my Distance to Empty App  * 
******************************************* 
Please enter the number of gallons of gas in the tank: 17.6
Please enter the fuel efficiency (miles per gallon): 24.8
Please enter the price of gas per gallon: 2.36

To drive 100 miles, it will cost $Infinity. ==> This is wrong, should be $9.52 
The car can currently drive a maximum of 0.0 miles. ==> This should be 436.48 miles
******************************************* 
* Thanks for using our App, Safe Travels! * 
******************************************* 

4 个答案:

答案 0 :(得分:3)

使用this指定您的意思是类字段,而不是构造函数参数,因为它们的名称相同。

public DT_CarStats(double gallons, double mpg, double ppg)
{
  this.gallons = gallons;
  this.mpg = mpg;
  this.ppg = ppg;
}

答案 1 :(得分:0)

gallons = gallons;
mpg = mpg;
ppg = ppg;

提示:在这段代码中,gallonsmpgppg是什么?

他们是参数!它们始终是参数,无论它们在=的哪一侧。 =左侧的变量名称未经过特殊处理。

要访问这些字段,请使用this

this.gallons = gallons;
this.mpg = mpg;
this.ppg = ppg;

或从字段中为参数指定不同的名称。

答案 2 :(得分:0)

这里的实例变量被方法参数覆盖。因此,要使用方法参数区分实例变量(当两个名称相同时)使用" this"构造函数中的关键字。

 this.gallons = gallons;

在您的情况下,方法参数再次分配给一个值,保持实例变量不变,因此值错误

答案 3 :(得分:0)

在构造函数中,您指的是与传入参数同名的字段。你应该使用"这个"关键字,以确保您设置对象的变量 this.gallons =加仑 等