gpt4 book ai didi

java - 使用构造函数和获取另一个类的方法的问题

转载 作者:行者123 更新时间:2023-11-29 03:15:52 24 4
gpt4 key购买 nike

因此,当我尝试运行用户类时,我不断收到错误消息,说需要 double,但没有找到任何参数。我在第 17、40、42、44、46 和 48 行收到错误。它们都是表示需要 double 的错误。用通俗易懂的英语回答我们将不胜感激。

我的主课:

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 () {

}

但是,当您定义任何构造函数时,默认构造函数将被删除。因此,您类(class)中唯一的构造函数是

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 - 使用构造函数和获取另一个类的方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com