好的,下面是我正在上的一门课程的代码...我需要知道如何正确编码该产品的总成本,但我终生无法弄清楚如何正确执行此操作。每个产品的价格是 1 美元,我还需要计算 7% 的销售税。帮助!!!
导入 javax.swing.JOptionPane;
/** * @author bary */ public class 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