打印购物小票
循环输入商品编号和购物数量
当输入n时结账
结账时计算应付金额并找零
折扣为0.8时找零
1.T恤价格:(245) 2.网球鞋价格:(300) 3.网球拍价格:(150)
考虑到用户不一定只买一种商品,所以这边就需要用到嵌套循环结构;while和switch
while语法:
int x = 1
while(){
}
switch语法:
int y = 1
switch(){
case 1 :
break;
case 2 :
break;
default:
break;
}
以及scanner的导入
例如:
package gwxp;
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class xx9 {
public static void main(String[] args) {
int m1 = 0;
int m2 = 0;
int m3 = 0;
System.out.println("MyShopping管理系统>购物结算");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
Scanner sc = new Scanner(System.in);
String i ="Y" ;
while("Y".equals(i)){
System.out.print("请输入商品编号:");
int spbh = sc.nextInt();
switch(spbh){
case 1 :
System.out.print("请输入数量:");
int tNum = sc.nextInt();
System.out.println("T恤¥245 "+"数量 "+tNum+" 合计"+(tNum*245));
m1=tNum*245;
break;
case 2 :
System.out.print("请输入数量:");
int xNum = sc.nextInt();
System.out.println("网球鞋¥300 "+"数量 "+xNum+" 合计"+(xNum*300));
m2=xNum*300;
break;
case 3 :
System.out.print("请输入数量:");
int pNum = sc.nextInt();
System.out.println("网球拍¥150 "+"数量 "+pNum+" 合计"+(pNum*150));
m3 = pNum*150;
break;
default:
System.out.print("输入错误");
break;
}
System.out.print("是否继续(Y/N)");
i = sc.next();
}
System.out.println("折扣 0.8");
System.out.println("应付金额:"+(m1+m2+m3)*0.8);
System.out.println("实付金额:");
int money =sc.nextInt();
System.out.println("找钱:"+(money-(m1+m2+m3)*0.8));
}
public static void main(String[] args) {
int m1 = 0;
int m2 = 0;
int m3 = 0;
System.out.println("MyShopping管理系统>购物结算");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
Scanner sc = new Scanner(System.in);
String i ="Y" ;
while("Y".equals(i)){
System.out.print("请输入商品编号:");
int spbh = sc.nextInt();
switch(spbh){
case 1 :
System.out.print("请输入数量:");
int tNum = sc.nextInt();
System.out.println("T恤¥245 "+"数量 "+tNum+" 合计"+(tNum*245));
m1=tNum*245;
break;
case 2 :
System.out.print("请输入数量:");
int xNum = sc.nextInt();
System.out.println("网球鞋¥300 "+"数量 "+xNum+" 合计"+(xNum*300));
m2=xNum*300;
break;
case 3 :
System.out.print("请输入数量:");
int pNum = sc.nextInt();
System.out.println("网球拍¥150 "+"数量 "+pNum+" 合计"+(pNum*150));
m3 = pNum*150;
break;
default:
System.out.print("输入错误");
break;
}
System.out.print("是否继续(Y/N)");
i = sc.next();
}
System.out.println("折扣 0.8");
System.out.println("应付金额:"+(m1+m2+m3)*0.8);
System.out.println("实付金额:");
int money =sc.nextInt();
System.out.println("找钱:"+(money-(m1+m2+m3)*0.8));
}
}