首页
 

通知公告

超市购物程序设计(java)

来源:欧亿体育点击:时间:2024-01-19 06:04

编写一个超市购物程序,在一家超市有牙刷、毛巾、水杯、苹果和香蕉五种商品,用户输入商品序列号进行商品购买,用户输入购买数量后计算出所需要花费的钱,一次购买结束后,需要用户输入“Y”或“N”,“Y”代表继续购买,“N”代表购物结束。商品价格如下表所示。

编号

商品名称

价格

1

牙刷

8.8

2

毛巾

10.0

3

水杯

18.8

4

苹果

12.5

5

香蕉

15.5

编程源代码:

package com.java;
import java.util.Scanner;
public class HelloWorld{
    public static void main(String[] args){
        String a = "Y";
        double sum = 0.0;
        do{
                System.out.println("请输入购买的编号:");
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();
                switch (num) {
                    case (1):
                        System.out.println("请输入购买牙刷的数量:");
                        int num1 = sc.nextInt();
                        sum += num1 * 8.8;
                        System.out.println("是否继续购物:");
                        a = sc.next();
                        break;
                    case (2):
                        System.out.println("请输入购买毛巾的数量:");
                        int num2 = sc.nextInt();
                        sum += num2 * 10;
                        System.out.println("是否继续购物:");
                        a = sc.next();
                        break;
                    case (3):
                        System.out.println("请输入购买水杯的数量:");
                        int num3 = sc.nextInt();
                        sum += num3 * 18.8;
                        System.out.println("是否继续购物:");
                        a = sc.next();
                        break;
                    case (4):
                        System.out.println("请输入购买苹果的数量:");
                        int num4 = sc.nextInt();
                        sum += num4 * 12.5;
                        System.out.println("是否继续购物:");
                        a = sc.next();
                        break;
                    case (5):
                        System.out.println("请输入购买香蕉的数量:");
                        int num5 = sc.nextInt();
                        sum += num5 * 15.5;
                        System.out.println("是否继续购物:");
                        a = sc.next();
                        break;
                }
        }while(a.equals("Y"));
        System.out.println("这次购物的总价是:");
        System.out.println(sum);
    }
}

运行结果截图: