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

来源:互联网 发布:winpe带网络 编辑:程序博客网 时间:2024/06/02 00:36
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();    }
0 0
原创粉丝点击