我们刚刚学习了新方法,我的教授希望我们调用该方法 3 次来计算珠宝店的不同税额。这是说明的片段:
a.使用提示和扫描仪读取 DiamondCost、settingCost 和 numOrdered 的值 b.通过添加diamondCost和settingCost中的值来计算基本成本 c.通过调用传入 baseCost 和 LuxuryRate 作为参数的 calcExtraCost 方法来计算 LuxuryTax。 d.通过调用传入 baseCost 和 stateRate 作为参数的 calcExtraCost 方法来计算 stateTax。 e.通过调用 calcExtraCost 方法并传入 baseCost 和 labourRate 作为参数来计算 labourCost。
我不断收到上述编译器错误,但我一生都无法弄清楚为什么。据我所知,我已将所有内容声明为双重。这是到目前为止我的代码:
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Locale;
import java.text.NumberFormat;
public class Project6
{
public static final double LUXURY_RATE = 0.2;
public static final double STATE_RATE = 0.10;
public static final double LABOR_RATE = 0.05;
//This is the main method, taking user input and displaying values from calcExtraCost method
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
NumberFormat dollar = NumberFormat.getCurrencyInstance(Locale.US);
int numOrdered = 0;
double diamondCost = 0.0;
double settingCost = 0.0;
double baseCost = 0.0;
double totalCost = 0.0;
double laborCost = 0.0;
double stateTax = 0.0;
double luxuryTax = 0.0;
double finalAmountDue = 0.0;
System.out.println("What is the cost of the diamond?");
diamondCost = keyboard.nextDouble();
System.out.println("What is the setting cost of the diamond?");
settingCost = keyboard.nextDouble();
System.out.println("How many would you like to order?");
numOrdered = keyboard.nextInt();
baseCost = (diamondCost + settingCost);
luxuryTax = calcExtraCost(baseCost, LUXURY_RATE);
}
public static void calcExtraCost(double bseCost, double rate)
{
double total = (bseCost * rate);
}
}
我需要调用这个方法来计算上面提到的变量,但不断收到所述编译器错误。我已经彻底检查并询问了我认识的每个人,但似乎无法找到我犯了什么错误的答案。我是java新手,所以我希望有人能帮助我理解我做错了什么。到目前为止,我只尝试使用 calcExtraCost 方法计算 LuxuryTax,但由于编译器错误,我无法继续。据我所知,所有内容都已声明为双精度,所以我不知道为什么它返回为空。
您的
calcExtraCost
应返回 double 类型。由于您在此处分配它:luxuryTax = calcExtraCost(baseCost, LUXURY_RATE);
,因此void
方法不能分配给任何其他变量。改变这个方法:
public static void calcExtraCost(double bseCost, double rate)
{
double total = (bseCost * rate);
}
致:
public static double calcExtraCost(double bseCost, double rate)
{
double total = (bseCost * rate);
return total;
}
设置你的函数的返回类型
public static double calcExtraCost(double bseCost, double rate)
{
return (bseCost * rate);
}