-6

我做了一个应用程序,我计算珠宝店的成本并使用主要方法中的方法。

此方法必须使用 3 次,仅更改一个参数。我还不断收到来自 Java 的错误,说方法和变量已经定义,

这是我的代码:

import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class JewelleryStore
{

    public static void main(String[] args)
    {

        double stateRate = 0.1;
        double luxuryRate = 0.2;
        double laborRate = 0.05;
        double extraCharge;
        int numOrdered;
        double diamondCost;
        double settingCost;
        double baseCost;
        double totalCost;
        double laborCost;
        double stateTax;
        double luxuryTax;
        double finalAmountDue;

        Scanner keyInput = new Scanner(System.in);

        System.out.println("What is the cost of the diamond?");
        diamondCost = keyInput.nextDouble();
        System.out.println("What is the cost of the setting?");
        settingCost = keyInput.nextDouble();
        System.out.println("How many rings are you ordering?");
        numOrdered = keyInput.nextInt();

        baseCost = diamondCost + settingCost;
        calcExtraCost(baseCost, laborRate);
        laborCost = extraCharge;
        calcExtraCost(baseCost, stateRate);
        stateTax = extraCharge;
        calcExtraCost(baseCost, luxuryRate);
        luxuryTax = extraCharge;
        totalCost = baseCost + laborCost + stateTax + luxuryTax;
        finalAmountDue = numOrdered*totalCost;
        DecimalFormat dollar = new DecimalFormat("0.00");
        JOptionPane.showMessageDialog(null, "Jasmine Jewelry INC:  TOTAL COST  BREAKDOWN" + "\nDiamond Cost: $" +dollar.format(diamondCost) + "\nSetting Cost: $" + dollar.format(settingCost) + "\nState Tax @ 10%: $" + dollar.format(stateTax) + "\nLuxury Tax  @ 20%: $" + dollar.format(luxuryTax)+"\nLabor Charge @ 5%: $"+dollar.format(laborCost)+"\nTotal Price: $" + dollar.format(diamondCost+settingCost+stateTax+luxuryTax+laborCost) +"\n\nNumberOrdered: " + numOrdered + "\n\nAmount Due $" + dollar.format(finalAmountDue));

    }
    public static double calcExtraCost(double diamond, double rate)
    {
        double extraCharge = diamond*rate;
        double diamond = baseCost;
        double rate = laborCost;

    }
    public static double calcExtraCost(double diamond2, double rate2)
    {
        double extracharge = diamond2*rate2;
        double diamond = baseCost;
        double rate2 = stateTax;
    }
    public static double calcExtraCost(double diamond2, double rate3)
    {
        double extracharge = diamond1*rate3;
        double diamond2 = baseCost;
        double rate3 = luxuryTax;
    }
}
4

3 回答 3

2

哇,好吧,你是 Java 新手。

编写一个计算一次额外成本的方法,并使用不同的参数多次调用它,您将得到不同的结果。

像这样的东西:

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

您可以根据以下某个比率为原始值添加一个百分比:

baseCost = baseCost + calcExtraCost(baseCost, laborRate);

我也推荐 Head First Java!

于 2013-03-20T12:53:27.783 回答
0

您基本上是一次又一次地创建相同的方法,这是不需要的......只需创建一种方法,该方法可以在运行时获取不同的值 伪代码:

public static double calcExtraCost(double diamond, double rate)
 {
       // do something
return somedouble value computed
               }

只是在调用该方法时,您需要传递不同的速率作为输入calcExtraCost(diamond,rate1) or calcExtraCost(diamond,rate2) or calcExtraCost(diamond,rate3 )等,JVM 足够聪明,可以调用相应的方法。另外,您的逻辑毫无意义,请阅读http://docs.oracle.com/javase/tutorial/上的 java 基础知识

于 2013-03-20T12:52:34.493 回答
0

我想这就是你所期望的

import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class JewelleryStore
{

    public static void main(String[] args)
    {

        double stateRate = 0.1;
        double luxuryRate = 0.2;
        double laborRate = 0.05;
        double extraCharge;
        int numOrdered;
        double diamondCost;
        double settingCost;
        double baseCost;
        double totalCost;
        double laborCost;
        double stateTax;
        double luxuryTax;
        double finalAmountDue;

        Scanner keyInput = new Scanner(System.in);

        System.out.println("What is the cost of the diamond?");
        diamondCost = keyInput.nextDouble();
        System.out.println("What is the cost of the setting?");
        settingCost = keyInput.nextDouble();
        System.out.println("How many rings are you ordering?");
        numOrdered = keyInput.nextInt();

        baseCost = diamondCost + settingCost;
        extraCharge=calcExtraCost(baseCost, laborRate);
        laborCost = extraCharge;
        extraCharge=calcExtraCost(baseCost, stateRate);
        stateTax = extraCharge;
        extraCharge=calcExtraCost(baseCost, luxuryRate);
        luxuryTax = extraCharge;
        totalCost = baseCost + laborCost + stateTax + luxuryTax;
        finalAmountDue = numOrdered*totalCost;
        DecimalFormat dollar = new DecimalFormat("0.00");
        JOptionPane.showMessageDialog(null, "Jasmine Jewelry INC:  TOTAL COST  BREAKDOWN" + "\nDiamond Cost: $" +dollar.format(diamondCost) + "\nSetting Cost: $" + dollar.format(settingCost) + "\nState Tax @ 10%: $" + dollar.format(stateTax) + "\nLuxury Tax  @ 20%: $" + dollar.format(luxuryTax)+"\nLabor Charge @ 5%: $"+dollar.format(laborCost)+"\nTotal Price: $" + dollar.format(diamondCost+settingCost+stateTax+luxuryTax+laborCost) +"\n\nNumberOrdered: " + numOrdered + "\n\nAmount Due $" + dollar.format(finalAmountDue));

    }
    static double calcExtraCost(double diamond, double rate)
    {
        double extraCharge = diamond*rate;
        return extraCharge;

    }
}
于 2013-03-20T13:15:54.393 回答