以下是我正在上课的代码...我需要知道如何正确编写此产品的totalCost代码,我不能为我的生活找出如何正确地执行此操作。每件产品的价格是1美元,我也需要计算7%的销售税。帮助!
导入javax.swing.JOptionPane;
/ ** * @author bary * / 公共课CocaCola1 {
public static void main(String[] args)
{
String openingMsg = "*** Welcome to CocaCola Online Ordering System ***\n"
+ "Do yourself a favor and order some Cola... NOW!";
JOptionPane.showMessageDialog(null, openingMsg);
System.out.println(0);
String name = getUserInput("What is your name?");
System.out.println(name);
String returnCustomer = getUserInput("Are you a returning customer? (yes/no)");
System.out.println(returnCustomer);
String orderType = getUserInput("Would you like to purchase Dasani, Coke, Diet Coke, or Cherry Coke?");
System.out.println(orderType);
String orderAmount = getUserInput("How many units would you like to purchase?");
System.out.println(orderAmount);
Integer.parseInt(orderAmount);
// each unit costs $1.00 so orderAmount is equal to cost per unit
double salesTax = 0.7;
// create and display output string
String outputMsg
= "Hello " + name + ".\n\n" +
"Your return customer status is " + returnCustomer + ".\n" +
"You ordered " + orderAmount +
" units of " + orderType + ".\n" +
"Your total cost is $" + orderAmount + salesTax + ".\n" +
"Thank you for visiting CocaCola" + ".\n\n" +
"Your order will be proccessed ASAP.\n";
JOptionPane.showMessageDialog(null, outputMsg);
}
private static String getUserInput(String prompt)
{
int failCount = 0;
do
{
String answer = JOptionPane.showInputDialog(prompt);
if (answer == null)
{
System.exit(0);
}
answer = answer.trim();
if (answer.isEmpty())
{
JOptionPane.showMessageDialog(null, "You must provide a non-blank answer");
failCount = failCount + 1;
} else
{
return answer;
}
} while (failCount < 3);
JOptionPane.showMessageDialog(null, "You failed three times to provide input... Try again later!");
System.exit(0);
return null; // means nothing, as I just exited on the prior line, but need it to compile
} // end main()
} //结束课CocaCola1
答案 0 :(得分:-1)
orderAmount + salesTax仅在销售税为静态时才会出现。您需要将orderAmount乘以销售税并添加salestax
ex:orderAmount * salesTax + prderAmount
或将salesTax设置为1.7即可 orderAmount *销售税