在java中如何用while循环,实现购买和付款功能

需求:/**需求:

 * 购买商品,单次只能选一个,
 * y为继续选购,
 * n为不继续选购,跳转到结账页面,
 * 跳转到结账页面时默认打八折。
 * 最后输出实付金额和不打折钱的订单金额
 * 
 * 1. 输出商品
 * 2。 设置商品值
 * 3。 计算商品折后价
 *
 * t恤245元
 * 网球鞋570元
 * 网球拍320元

代码如下(示例):


import java.util.Scanner;

/**
 * @author 
 * {@data 2022/11/22}
 */
public class Calshoping {
    public static void main(String[] args) {

        /**
         * 定义标识是否继续
         */
        String answer = "y";

        /**
         * 定义购买数量
         */
        int amount = 0;

        /**
         * 定义商品价格
         */
        int price = 0;

        /**
         *定义商品总价
         */
        double total = 0.0;

        /**
         * 定义商品名字
         */
        String name = "";

        /**
         * 定义打折力度
         */
        double sale = 0.8;

        /**
         * 定义实际付款金额
         */
        int payment = 0;

        Scanner sc = new Scanner(System.in);

        while (("y").equals(answer)) {
            /**
             * 把你要循环的所有条件都写在while循环中
             */
            System.out.println("请选择购买商品编号:");
            System.out.println("1,T恤      2,网球鞋     3,网球拍");
            System.out.println("**************************************************");
            System.out.println("请输入商品编号:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    price = 245;
                    name = "T恤";
                    break;
                case 2:
                    price = 570;
                    name = "网球鞋";
                    break;
                case 3:
                    price = 320;
                    name = "网球拍";
                    break;
                default:
                    System.out.println("输入错误,请输入正确数字");
                    break;
            }
            /**
             * 做完switch选择后,输出,否则就在switch选择结构中
             */
            System.out.println("请输入购买数量:");
            amount = sc.nextInt();
            total = price * amount;
            System.out.println("是否继续:(y/n)");
            answer = sc.next();

            /**
             * 做判断,如果y继续的话,则循环一遍while循环,while循环true=y
             */
            if (("y").equals(answer)) {
                answer = "y";
                System.out.println(name + "¥" + price + "\t数量:" + amount + "合计:" + total);
            }

            /**
             * 做判断,如果是n的话结束循环,输出
             */

            else if (("n").equals(answer)) {
                System.out.println("为您跳转到买单界面:");
                System.out.println("折扣:" + sale);
                System.out.println("应付金额:" + (total * sale));
                System.out.println("实付金额:");
                payment = sc.nextInt();
                double reback = payment-(total*sale);
                System.out.println("找零:"+reback);
                answer = "n";
            }
        }
        /**
         * 如果
         */
          System.out.println("欢迎下次光临");
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值