我做了一个应用程序,我计算珠宝店的成本,并在主要方法中使用方法。
此方法必须使用三次,只更改一个参数。我也一直从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;
}
}
答案 0 :(得分:2)
编写一个计算额外成本一次的方法,并根据需要多次调用它,使用不同的参数,您将得到不同的结果。
类似的东西:
public static double calcExtraCost(double value, double rate)
{
return value*rate;
}
您可以根据以下某种比率为原始值添加一个百分比:
baseCost = baseCost + calcExtraCost(baseCost, laborRate);
我也推荐Head First Java!
答案 1 :(得分: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/
答案 2 :(得分: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;
}
}