所以当我尝试运行用户类时,我不断收到错误,说需要double,没有找到参数。我在第17,40,42,44,46和48行遇到错误。他们认为所有错误都表示需要双打。用简单的英语答案将不胜感激。
我的主要课程:
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;
}
}
答案 0 :(得分:2)
在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);
答案 1 :(得分:1)
除了Sleiman Jneidi的回答,你正在调用函数,但不提供任何参数,因为方法定义要求:
double standingCharge = myCalculator.standingCharge();
需要更改为:
double standingCharge = myCalculator.standingCharge(10.0); //example for 10 days
代码的第42,44,46,48行中的同样问题
答案 2 :(得分:1)
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(... , ...)
的返回值。
double costBeforeVAT = ...;
// pass argument here
// v
double VAT = myCalculator.VAT( costBeforeVAT );
请注意,在某些情况下,您可能不需要这些方法来获取某些参数,例如
public double standingCharge () {
return numberOfDays * 0.25;
}
因为numberOfDays
已经是班级ElectricityCalculator
和
public double costBeforeVAT () {
return costOfElectricity() + standingCharge();
}
因为可以直接调用这些方法,而不是要求传递结果。