-6

宝石店のコストを計算し、main メソッド内のメソッドを使用するアプリケーションを作成します。

このメソッドは、1 つのパラメーターのみを変更して 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 の初心者です。

余分なコストを 1 回計算するメソッドを作成し、別のパラメーターで何度でも呼び出すと、別の結果が返されます。

このようなもの:

 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

基本的に、必要のない同じメソッドを何度も作成しています...メソッドを1つ作成するだけで、メソッドは実行時に異なる値を取得でき ます

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 に答える