java中数组的运用制作一个简单的购物程序

public static void main(String[] args) {

        String[] phoneName = {"华为G9","荣耀5x","华为笔记本","荣耀5c"};
        int[] phonePrice = {1699,1099,6688,899};
        int[] cost = new int[4] ;//每一个产品需要的花费
        int[] count =new int[4];//每个产品的数量
        int isContinue =0;
        int sumCost = 0;
        Scanner mScanner = new Scanner(System.in);
        do {
            System.out.println("请选择你要购买的产品 ");
            for (int i = 0; i < count.length; i++) {
                System.out.print((i + 1) + ":" + phoneName[i] + "的价格为:"+ phonePrice[i] + "   ");
            }
            System.out.println();

            int num1 = mScanner.nextInt();// 接受购买的产品号
            System.out.println("请输入需要购买的数量");
            int num2 = mScanner.nextInt();// 接受购买的数量

            //计算每个商品购买的数量和每一个商品所需要的花费
            switch ((num1 - 1)) {
            case 0:
                count[0] += num2;
                cost[0] += count[0] * phonePrice[0];
                break;
            case 1:
                count[1] += num2;
                cost[1] += count[1] * phonePrice[1];
                break;
            case 2:
                count[2] += num2;
                cost[2] += count[2] * phonePrice[2];
                break;
            case 3:
                count[3] += num2;
                cost[3] += count[3] * phonePrice[3];
                break;

            default:
                System.out.println("输入有误请新选择编号:");
                break;
            }

            System.err.println("是否继续购买 1继续 ,0退出");
            isContinue = mScanner.nextInt();
        } while (isContinue == 1);
        System.out.println("**********************消费清单**************************");
        System.out.println("购买的产品名\t产品的价格\t购买的数量\t购买该产品所需要的花费");
        for (int i = 0; i < count.length; i++) {
            System.out.println(phoneName[i]+"\t\t"+phonePrice[i]+"\t"+count[i]+"\t"+cost[i]);
            sumCost +=cost[i];
        }

        System.out.println("总消费为:"+sumCost);
        mScanner.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值