Java 使用构造函数和获取另一个类的方法时出现问题
因此,当我试图运行user类时,我不断地得到错误,说double是必需的,并且找不到参数。我在第17、40、42、44、46和48行发现了错误。这些都是错误,都说需要双倍。请用简单的英语回答 我的主要班级:Java 使用构造函数和获取另一个类的方法时出现问题,java,constructor,Java,Constructor,因此,当我试图运行user类时,我不断地得到错误,说double是必需的,并且找不到参数。我在第17、40、42、44、46和48行发现了错误。这些都是错误,都说需要双倍。请用简单的英语回答 我的主要班级: import java.util.Scanner; public class ElectricityCalculatorUser { //Main method public static void main (String [] args) { El
import java.util.Scanner;
public class ElectricityCalculatorUser {
//Main method
public static void main (String [] args) {
ElectricityCalculator myCalculator = new ElectricityCalculator();
Scanner input = new Scanner (System.in);
//Input the initial reading
double initialReading;
System.out.print ("What is the inital reading on your electricity meter in kwH? ");
initialReading = input.nextDouble ();
//Input the final reading
double finalReading;
System.out.print ("What is the final reading on your electricity meter in kwH? ");
finalReading = input.nextDouble ();
//Input the number of days
//between readings
double numberOfDays;
System.out.print ("How many days have passed between your initial and final reading? ");
numberOfDays = input.nextDouble ();
//Calculations
double totalElectricityUsed = myCalculator.totalElectricityUsed();
System.out.print ("Total electricity used = " + totalElectricityUsed);
double costOfElectricity = myCalculator.costOfElectricity();
System.out.print ("Cost of electricity = " + costOfElectricity);
double standingCharge = myCalculator.standingCharge();
System.out.print ("Standing charge = " + standingCharge);
double costBeforeVAT = myCalculator.costBeforeVAT();
System.out.print ("Cost before VAT is added = " + costBeforeVAT);
double VAT = myCalculator.VAT();
System.out.print ("Cost of VAT = " + VAT);
double totalCost = myCalculator.totalCost();
System.out.print ("Total cost = " + totalCost);
}
}
我的课程包括所有的方法:
public class ElectricityCalculator {
//Attributes
private double initialReading;
private double finalReading;
private double numberOfDays;
//Constructors
public ElectricityCalculator (double ir, double fr, double nod) {
initialReading = ir;
finalReading = fr;
numberOfDays = nod;
}
//Calculating total electricity used
public double totalElectricityUsed () {
return finalReading - initialReading;
}
//Calculating cost of electricity
public double costOfElectricity () {
return totalElectricityUsed * 0.175;
}
//Calculating standing charge
public double standingCharge (double numberOfDays) {
return numberOfDays * 0.25;
}
//Calculating cost before VAT is added
public double costBeforeVAT (double costOfElectricity, double standingCharge) {
return costOfElectricity + standingCharge;
}
//Cost of VAT
public double VAT (double costBeforeVAT) {
return costBeforeVAT * 0.05;
}
//Total cost of electricity used
//including VAT
public double totalCost (double costBeforeVAT, double VAT) {
return costBeforeVAT + VAT;
}
}
在java中,如果不编写构造函数,将自动为您添加一个默认构造函数,该构造函数将是
public
,不带任何参数
如下所示:
public ElectricityCalculator () {
}
但是,当您定义任何构造函数时,默认构造函数将被删除。因此,类中唯一的构造函数是
public ElectricityCalculator (double ir, double fr, double nod) {
initialReading = ir;
finalReading = fr;
numberOfDays = nod;
}
因此
ElectricityCalculator myCalculator = new ElectricityCalculator();
不匹配任何构造函数
您只需在获得构造对象所需的值后创建实例
ElectricityCalculator myCalculator = new ElectricityCalculator(initialReading, finalReading, numberOfDays);
在java中,如果不编写构造函数,将自动为您添加一个默认构造函数,该构造函数将是
public
,不带任何参数
如下所示:
public ElectricityCalculator () {
}
但是,当您定义任何构造函数时,默认构造函数将被删除。因此,类中唯一的构造函数是
public ElectricityCalculator (double ir, double fr, double nod) {
initialReading = ir;
finalReading = fr;
numberOfDays = nod;
}
因此
ElectricityCalculator myCalculator = new ElectricityCalculator();
不匹配任何构造函数
您只需在获得构造对象所需的值后创建实例
ElectricityCalculator myCalculator = new ElectricityCalculator(initialReading, finalReading, numberOfDays);
除了Sleiman Jneidi answer,您正在调用函数,但不提供任何参数,因为方法定义要求:
double standingCharge = myCalculator.standingCharge();
需要更改为:
double standingCharge = myCalculator.standingCharge(10.0); //example for 10 days
代码第42行、第44行、第46行和第48行中存在相同的问题除了Sleiman Jneidi answer之外,您正在调用函数,但不提供任何参数,正如方法定义所要求的:
double standingCharge = myCalculator.standingCharge();
public ElectricityCalculator (double ir, double fr, double nod);
public double standingCharge (double numberOfDays);
public double costBeforeVAT (double costOfElectricity, double standingCharge);
public double VAT (double costBeforeVAT);
public double totalCost (double costBeforeVAT, double VAT);
需要更改为:
double standingCharge = myCalculator.standingCharge(10.0); //example for 10 days
代码的第42、44、46、48行中也存在同样的问题
public ElectricityCalculator (double ir, double fr, double nod);
public double standingCharge (double numberOfDays);
public double costBeforeVAT (double costOfElectricity, double standingCharge);
public double VAT (double costBeforeVAT);
public double totalCost (double costBeforeVAT, double VAT);
构造函数和这些方法接受参数,但您试图调用它们,就好像它们没有一样
对于构造函数,只需移动这一行即可
ElectricityCalculator myCalculator = new ElectricityCalculator();
在从用户获取输入后执行,以便可以传入参数
// pass arguments here
// v v v
... = new ElectricityCalculator( initialReading , finalReading , numberOfDays );
对于其他方法,您需要传递中期计算的结果。例如,VAT(…)
接受一个costBeforeVAT
,我假设它应该是costBeforeVAT(…,…)
的返回值
请注意,在某些情况下,您可能不需要这些方法来具有某些参数,例如
public double standingCharge () {
return numberOfDays * 0.25;
}
因为numberOfDays
已经是ElectricityCalculator
类的成员,并且
public double costBeforeVAT () {
return costOfElectricity() + standingCharge();
}
因为这些方法可以直接调用,而不是要求传递它们的结果
相关:
构造函数和这些方法接受参数,但您试图调用它们,就好像它们没有一样
对于构造函数,只需移动这一行即可
ElectricityCalculator myCalculator = new ElectricityCalculator();
在从用户获取输入后执行,以便可以传入参数
// pass arguments here
// v v v
... = new ElectricityCalculator( initialReading , finalReading , numberOfDays );
对于其他方法,您需要传递中期计算的结果。例如,VAT(…)
接受一个costBeforeVAT
,我假设它应该是costBeforeVAT(…,…)
的返回值
请注意,在某些情况下,您可能不需要这些方法来具有某些参数,例如
public double standingCharge () {
return numberOfDays * 0.25;
}
因为numberOfDays
已经是ElectricityCalculator
类的成员,并且
public double costBeforeVAT () {
return costOfElectricity() + standingCharge();
}
因为这些方法可以直接调用,而不是要求传递它们的结果
相关:。那么,我该如何修复代码,使其基本上按我想要的方式运行呢?对编程来说非常陌生,所以我不太理解你的答案。@John我已经编辑了答案,现在应该很清楚了。那么我该如何修复代码,使其基本上按照我想要的方式运行呢?对编程非常陌生,所以我不太理解你的答案。@John我编辑了答案,现在应该很清楚了,但我希望用户输入主方法中的3个值,然后从中计算出所有其他方法。这就是为什么需要使用参数化构造函数
ElectricityCalculator myCalculator=new ElectricityCalculator(初始读数、最终读数、天数)代码>但我希望用户输入主方法中的3个值,然后从中计算出所有其他方法。这就是为什么需要使用参数化构造函数ElectricityCalculator myCalculator=new ElectricityCalculator(初始读数、最终读数、天数)代码>