该计划的目的是打印以迎接客户,然后向他们提供有关待售物品和价格的信息,然后询问客户想要的每件物品的数量。
该程序应该能够显示总数并询问客户支付的金额,并最终显示包含物品,物品价格,数量,产品成本,小计,折扣,税,总额的收据,付款和客户的变化。
商店物品的定价为:T恤18.95美元,薯片袋1.79美元,12包可乐售价2.99美元(1.20美元押金)。
除可口可乐外的所有商品均以低于标价15%的价格出售。 T恤还征收6%的密歇根州销售税。
我在编写程序和使用java方面相当新,所以所有的帮助都将受到赞赏。这是我到目前为止为代码编写的内容:
import java.util.Scanner;
//The purpose of this program is to simulate the shopping process by calculating the costs of the items
//and producing a receipt for the shopping trip.
//CPS 180
//Joseph Eastman
//September 24, 2014
public class StoreReceipt {
static final double TSHIRT_PRICE = 16.1075;
static final double CHIPS_PRICE = 1.5215;
static final double COKE_PRICE = 2.99;
String a;
static int numberShirts;
static int numberChips;
static int numberCoke;
static double tshirtTotal = TSHIRT_PRICE * numberShirts;
static double chipsTotal = CHIPS_PRICE * numberChips;
static double cokeTotal = (COKE_PRICE + 1.20) * numberCoke;
static double finalTotal = tshirtTotal + chipsTotal + cokeTotal;
{
}
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("What's your name?");
String a = input.nextLine();
System.out.println("Welcome to Denny's Market, " + a + "! We have the following items for sale:");
System.out.println("T-shirt $18.95 15% off");
System.out.println("Chips $1.79 15% off");
System.out.println("Coke $2.99");
System.out.println("How many T-shirts do you want?");
String numberShirts = input.nextLine();
System.out.println("How many bags of potato chips?");
String numberChips = input.nextLine();
System.out.println("What about 12-pack coke?");
String numberCoke = input.nextLine();
tshirtTotal = tshirtTotal * .85;
chipsTotal = chipsTotal * .85;
tshirtTotal = tshirtTotal * 1.06;
System.out.println("Your total is: " + finalTotal);
}
}
现在,当我输入输入时,我的总数为0,而且我说我没有使用任何变量。产品数量的输入可以是任何东西,它只需要正确计算总数和客户应该收到的变化。
以下是程序完成并运行后输出结果的示例:
What’s your name? John
Welcome to Denny’s Market, John! We have the following items for sale:
T-shirt $18.95 15% off
Chips $ 1.79 15% off
Coke $ 2.99
How many T-shirts do you want? 3
How many bags of potato chips? 4
What about 12-pack Coke? 2
Your total is $65.69.
Please enter your payment: 70
John, here is your receipt:
item unit price how many cost
--------------------------------------------------------
T-shirt 18.95 3 56.85
Chips 1.79 4 7.16
Coke 2.99 2 5.98
Deposit 2.40
Subtotal 72.39
Discount -9.60
Tax 2.90
----------
Total 65.69
Payment 70.00
Your Change 4.31
Thank you. Come Again!
答案 0 :(得分:1)
你好像已经有了一些(这很好)。让我们一步一步
T恤衫18.95美元
double tshirtTotal = TSHIRT_PRICE * numberShirts;
1.79美元一袋薯片
double chipsTotal = CHIPS_PRICE * numberChips;
12包可口可乐(2.20美元押金)<$> 2.99美元
double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ;
除可口可乐外的所有商品均以低于标价15%的价格出售。
tshirtTotal = tshirtTotal * .85;
chipsTotal = chipsTotal * .85;
T恤还征收6%的密歇根州销售税。
tshirtTotal = tshirtTotal * 1.06;
最后让我们做总计:
double finalTotal = tshirtTotal + chipsTotal + cokeTotal;
System.out.println("Your total is: " + finalTotal);
因此,您的main
方法应如下所示:
//part of your original code
input = new Scanner(System.in);
System.out.println("What's your name?");
String a = input.nextLine();
System.out.println("Welcome to Denny's Market, " + a + "! We have the following items for sale:");
System.out.println("T-shirt $18.95 15% off");
System.out.println("Chips $1.79 15% off");
System.out.println("Coke $2.99");
System.out.println("How many T-shirts do you want?");
String numberShirts = input.nextLine();
System.out.println("How many bags of potato chips?");
String numberChips = input.nextLine();
System.out.println("What about 12-pack coke?");
String numberCoke = input.nextLine();
//new code below
double tshirtTotal = TSHIRT_PRICE * numberShirts;
double chipsTotal = CHIPS_PRICE * numberChips;
double cokeTotal = (COKE_PRICE + 1.20) * numberCoke ;
tshirtTotal = tshirtTotal * .85;
chipsTotal = chipsTotal * .85;
tshirtTotal = tshirtTotal * 1.06;
double finalTotal = tshirtTotal + chipsTotal + cokeTotal;
System.out.println("Your total is: " + finalTotal);
然后要钱。如需更改,只需money-finalTotal
(假设用户总是输入足够的钱)。您需要做的其他事情就是格式化输出。我会把它留给你。
如果您担心将double
变量格式化为类似金钱的值,那么check this post out。