【超市购物程序设计】
1、案例描述
编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品,商品价格如表所示
编号 | 商品名称 | 价格(元) |
---|---|---|
1 | 牙刷 | 8.8 |
2 | 毛巾 | 10.0 |
3 | 水杯 | 18.8 |
4 | 苹果 | 12.5 |
5 | 香蕉 | 15.5 |
用户输入商品序列号进行商品购买,用户输入购买数量后计算所需要花费的钱。一次购买结束后,需要用户输入“Y”或“N”,“Y”代表继续购买,“N”代表购物结束
2、案例目的
(1)学会分析“超市购物程序设计”的案例实现思路
(2)根据思路完成“超市购物程序设计”案例的代码编写、编译及运行
(3)掌握在程序中使用while循环结构和switch循环结构语句进行运算操作。
3、运行结果
4、代码实现
package cn.itcast.example;
import java.text.CollationElementIterator;
import java.util.Scanner;
import javax.print.attribute.Size2DSyntax;
import javax.sql.rowset.spi.SyncFactoryException;
public class example {
public static void main (String[] arge) {
String select;
double toothbrush = 8.8;
double towel = 10.0;
double cup = 18.8;
double apple = 12.5;
double banana = 15.5;
System.out.println("商品购买清单");
System.out.println("1.牙刷");
System.out.println("2.毛巾");
System.out.println("3.水杯");
System.out.println("4.苹果");
System.out.println("5.香蕉");
do {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要购买商品的序号:");
int a = sc.nextInt();
switch (a) {
case (1):{
Scanner sy = new Scanner(System.in);
System.out.println("请输入购买商品数量:");
int s1 = sy.nextInt();
System.out.println("您一共消费" + s1*toothbrush + "元");;
break;
}
case (2):{
Scanner sm = new Scanner(System.in);
System.out.println("请输入购买商品数量:");
int s2 = sm.nextInt();
System.out.println("您一共消费" + s2*towel + "元");
break;
}
case (3):{
Scanner ss = new Scanner(System.in);
System.out.println("请输入购买商品数量:");
int s3 = ss.nextInt();
System.out.println("您一共消费" + s3*cup + "元");
break;
}
case (4):{
Scanner sp = new Scanner(System.in);
System.out.println("请输入购买商品数量:");
int s4 = sp.nextInt();
System.out.println("您一共消费" + s4*apple + "元");
break;
}
case (5):{
Scanner sx = new Scanner(System.in);
System.out.println("请输入购买商品数量:");
int s5 = sx.nextInt();
System.out.println("您一共消费" + s5*banana + "元");
break;
}
}
Scanner out = new Scanner(System.in);
System.out.println("请输入是否继续购买,若是请输入Y,若不是请输入N");
select = out.next();
} while (select.equalsIgnoreCase("y"));
}
}