JavaSE_day04

1、循环录入10位顾客的年龄,分别计算30以上和30岁以下的年龄比例

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        int x = 0, y = 0;
        for (int i = 0; i<arr.length; i++){
            System.out.println("请输入第"+(i+1)+"位顾客的年龄");
            arr[i] = sc.nextInt();

            if (arr[i]<30){
                x++;
            }else {
                y++;
            }
        }
        System.out.println("30岁以下的比例为"+(x*10)+"%");
        System.out.println("30岁以上的比例为"+(y*10)+"%");

    }
}

 2、综合运用嵌套if选择结构、switch选择结构、多重if选择结构进行实现商品换购功能

public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入消费金额:");
        double price = sc.nextDouble();
        System.out.println("是否参加优惠换购活动:");
        System.out.println("1: 满50元,加2元换购百事可乐饮料1瓶");
        System.out.println("2: 满100元, 加3元换购500m1可乐一 瓶");
        System.out.println("3:满100元,加10元换购5公斤面粉");
        System.out.println("4: 满200元,加10元可换购1个苏波尔炒菜锅");
        System.out.println("5: 满200元,加20元可换购欧莱雅爽肤水一瓶");
        System.out.println("0: 不换购");
        int num = sc.nextInt();
        System.out.println("请选择:"+num);
        double total = 0; //消费总金额
        switch (num){
            case 1:
                if (price>=50 && price<100){
                    total = price+2;
                }
                System.out.println("本次消费总金额:"+total);
                System.out.println("成功换购:百事可乐饮料一瓶。");
                break;
            case 2:
                if (price>=100 && price<200){
                    total = price+3;
                }
                System.out.println("本次消费总金额:"+total);
                System.out.println("成功换购:500ml可乐一瓶。");
                break;
            case 3:
                if (price>=100 && price<200){
                    total = price+10;
                }
                System.out.println("本次消费总金额:"+total);
                System.out.println("成功换购:5公斤面粉。");
                break;
            case 4:
                if (price>=200){
                    total = price+10;
                }
                System.out.println("本次消费总金额:"+total);
                System.out.println("成功换购:1个苏泊尔炒菜锅。");
                break;
            case 5:
                if (price>=200){
                    total = price+20;
                }
                System.out.println("本次消费总金额:"+total);
                System.out.println("成功换购:欧兰雅爽肤水一瓶。");
                break;
            case 0:
                total = price;
                System.out.println("本次消费总金额:"+total);
                System.out.println("不换购");
                break;
            default:
                System.out.println("没有这个选择");
        }
        System.out.println("程序结束");
    }
}

3、需求说明:循环输入商品编号和购买数量,当输入n时结账 ,结账时计算应付金额并找零  

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("****************************************");
        System.out.println("请输入购买的商品编号:");
        System.out.println("1、T恤      2 、网球鞋    3、网球拍");
        System.out.println("****************************************");

        String s = "y";
        while (s.equals("y")){

            System.out.print("请输入商品编号:");
            int productID = sc.nextInt();  //记录商品编号
            System.out.print("请输入购买数量:");
            int buyNum = sc.nextInt();   //记录购买数量

            if (productID==1){
                System.out.println("T恤¥245.0    数量"+buyNum+"    合计¥"+(245.0*buyNum));
            } else if (productID==2){
                System.out.println("网球鞋¥570.0   数量"+buyNum+"   合计¥"+(570.0*buyNum));
            } else if (productID==3) {
                System.out.println("网球拍¥250.0   数量"+buyNum+"   合计¥"+(250.0*buyNum));
            }

            System.out.print("是否继续(y/n)");   //判断是否继续执行
            String next = sc.next();

            if (next.equals("y")){
                System.out.println("请继续下单购买!");
            }else {
                System.out.println("折扣:0.8");
                System.out.println("应付金额:"+(245.0*buyNum+570.0*buyNum+250.0*buyNum)*0.8);
                System.out.print("实付金额:");
                double v = sc.nextDouble();
                System.out.println("找钱:"+(v-(245.0*buyNum+570.0*buyNum+250.0*buyNum)*0.8));
                break;
            }
        }
        System.out.println("程序结束,祝您购物愉快!");
    }
}

4、完成以下编程 

public class Test4 {
    public static void main(String[] args) {
        //1、创建数组存储商品名称
        String[] arr = new String[]{"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361腰包"};
        System.out.println("本次活动特价商品有:");
        //2、遍历数组输出商品名称
        for(String s : arr){
            System.out.println(s);
        }
    }
}

5、完成以下编程 

 

public class Test5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[] arr = new double[5];  //建立一个数组来存储输入的金额
        double sum = 0; //用来存取总金额
        for (int i = 0; i<5; i++){
            System.out.print("请输入第"+(i+1)+"笔购物金额:");
            double price = sc.nextDouble();
            arr[i] = price;  //将每次的金额赋值给数组中的元素
            sum += price;   //计算总金额
        }
        System.out.println("序号       金额(元)");
        for (int x=1,j=0; x<=5 && j<arr.length; x++,j++){ //遍历序号和数组进行打印
            System.out.println(x+"         "+arr[j]);
        }
        System.out.println("总金额     "+sum);

    }
}

 6、完成以下编程

public class Test6 {
    public static void main(String[] args) {
        int[] score = new int[]{72,89,65,58,87,91,53,82,71,93,76,68}; //存储12个学生的成绩
        int[] count = new int[5];     //存储每个评级的人数
        for (int x : score){
            if (x>=90){
                count[4]++;  //记录评级为A的人数
            }else if (x>=80 && x<90){
                count[3]++;  //记录评级为B的人数
            } else if (x>=70 && x<80) {
                count[2]++;  //记录评级为C的人数
            } else if (x>=60 && x<70) {
                count[2]++;  //记录评级为D的人数
            } else if (x>=0 && x<60) {
                count[0]++;  //记录评级为E的人数
            }
        }
        System.out.println("评级为A的人数有:"+count[4]+"人");
        System.out.println("评级为B的人数有:"+count[3]+"人");
        System.out.println("评级为C的人数有:"+count[2]+"人");
        System.out.println("评级为D的人数有:"+count[1]+"人");
        System.out.println("评级为E的人数有:"+count[0]+"人");
    }
}

 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值