标题实验 2-4 编写程序,模拟购物。
(1) 输入商品名称、单价、数量,计算商品价格,输出商品名称,单价,数量,总价。
(2) 当总价大于或等于500时,享受8折优惠。
(3) 模拟付款找零过程。
(4) 若付款金额不足,需要继续付款。
(5) 购买多件商品时,需要对多件商品的总价进行汇总,得到应付款额,请程序中实现。
方法一 方法二请在我博客中自行查看
import java.util.Scanner;
public class ship{
public ststic void main(String[]args){
int i = 0,a = 0;
double moneys = 0;
double newmony = 0;
System.out.print("请输入商品名称:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int c = name.length(); //c为测量"name"的长度
for(i = 0 ;i < c;i++) { //通过查找空格数来确定顾客购买几种商品
char ch = name.charAt(i);
if(ch == ' ') {
a++;
}
}
a += 1; //顾客一共购买a种商品
System.out.print("请输入商品单价:");
double money[] = new double[a]; //利用数组保存每一种商品的单价
for(i = 0;i < a; i++) {
money[i] = sc.nextDouble();
}
System.out.print("请输入商品的数量:"); //利用数组保存每一种商品的数量
Scanner as = new Scanner(System.in);
double e[] = new double[a];
for(i = 0;i < a; i++) {
e[i] = as.nextInt();
}
System.out.print("请付款:");
Scanner bs = new Scanner(System.in);
double p = bs.nextDouble(); //利用数组把单价和数量相乘得到总价
//System.out.println(a);
for(i = 0;i < a; i++) {
moneys = money[i] * e[i];
//System.out.println(e[i]);
//System.out.println(money[i]);
newmoney += moneys;
//System.out.println(newmoney);
}
if(newmoney >= 500) {
newmoney = newmoney * 0.8;
while(newmoney > p) {
System.out.println("对不起,您的付款不足,还需支付" + (newmoney - p) + "元");
break;
}
while(newmoney < p) {
System.out.println("尊敬的顾客您好,您本次消费" + newmoney + "元," +"收您" + p + "元," + "找您" + (p - newmoney) + "元,谢谢惠顾!");
break;
}
}
else {
while(newmoney > p) {
System.out.println("对不起,您的付款不足,还需支付" + (newmoney - p) + "元");
break;
}
while(newmoney < p) {
System.out.println("尊敬的顾客您好,您本次消费" + newmoney + "元," +"收您" + p + "元," + "找您" + (p - newmoney) + "元,谢谢惠顾!");
break;
}
}
}
}