发现编译器错误:void required:简单方法java赋值的double

时间:2014-03-13 05:25:12

标签: java methods compiler-construction

我们刚刚学会了新方法,我的教授希望我们称这种方法3次来计算珠宝店的不同税额。以下是说明的摘要:

一个。使用提示和扫描程序读入diamondCost,settingCost和numOrdered b的值。通过在diamondCost和settingCost中添加值来计算基本成本 C。通过调用calcExtraCost方法传递baseCost和luxuryRate作为参数来计算luxuryTax。 d。通过调用calcExtraCost方法传递baseCost和stateRate作为参数来计算stateTax。 即通过调用calcExtraCost方法传递baseCost和laborRate作为参数来计算laborCost。

我不断收到上面所述的编译器错误但是对于我的生活无法弄清楚原因。据我所知,我已宣布一切都是双重的。到目前为止,这是我的代码:

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,但由于编译器错误,我无法继续。据我所知,一切都被宣告为双重,所以我不知道为什么它会作为一个空洞返回。

2 个答案:

答案 0 :(得分:3)

您的calcExtraCost应该返回一种双重类型。由于您在此处进行分配: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;
}

答案 1 :(得分:0)

设置返回类型你的函数

public static double  calcExtraCost(double bseCost, double rate)
{
  return (bseCost * rate);
}