温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[JAVA]复习第五日JAVA-Shop案例(数组遍历练习)

发布时间:2020-08-11 09:21:46 来源:ITPUB博客 阅读:154 作者:Taiaishua 栏目:编程语言

点击(此处)折叠或打开

  1. /*
  2.    实现商品的库存管理
  3.      功能:
  4.      1. 展示用户选择功能清单
  5.         2. 根据选择的功能编号,进行不同的操作
  6.          A. 展示所有库存
  7.          B. 修改库存数量
  8.         
  9.      分析:
  10.      1. 展示用户清单:
  11.          输出语句, 用户输入, 选择功能序号
  12.         2. 根据选择,调用不同的方法
  13.          switch语句
  14.              case 1 2 3
  15.         
  16.          A 展示库存
  17.          将存储商品的数组,遍历
  18.          B 修改库存
  19.         
  20.              修改所有的库存数量
  21. */
  22. import java.util.Scanner;
  23. public class Shopp{
  24.     public static void main(String[] args){
  25.         //使用数组,保存商品的信息
  26.         //品名,尺寸,价格,库存数, 定义5个数组
  27.         String[] brand = {"MacBookAir","ThinkpadT450"};
  28.         double[] size = {13.3,15.6};
  29.         double[] price = {9998.97,6789.56};
  30.         int[] count = {0,0};
  31.         while(true){
  32.         int choose = chooseFunction();
  33.         switch(choose){
  34.             case 1:
  35.              //调用查看库存清单方法
  36.              printStore(brand,size,price,count);
  37.             break;
  38.             
  39.             case 2:
  40.              //调用修改库存的方法
  41.              update(brand,count);
  42.             break;
  43.             
  44.             case 3:
  45.              return ;
  46.             
  47.             
  48.             default:
  49.              System.out.println("没有这个功能");
  50.             break;
  51.         }
  52.         }
  53.     }
  54.     /*
  55.      定义方法,修改所有商品的库存
  56.      用户输入1个,修改1个
  57.         返回值,没有
  58.         参数, 库存数的数组, 品名数组
  59.     */
  60.     public static void update(String[] brand, int[] count){
  61.         //遍历数组,遍历到一个,修改一个
  62.         //接受键盘输入
  63.         Scanner sc = new Scanner(System.in);
  64.         //遍历数组
  65.         for(int i = 0; i < brand.length ; i++){
  66.             System.out.println("请输入"+brand[i]+"的库存数");
  67.             //键盘输入,录入库存, 存储到库存的数组中
  68.             int newCount = sc.nextInt();
  69.             count[i] = newCount;
  70.         }
  71.         //int chooseNumber = sc.nextInt();
  72.     }
  73.     
  74.     /*
  75.      定义方法,展示所有的库存清单,遍历
  76.      返回值,没有
  77.      参数, 数组
  78.     */
  79.     public static void printStore(String[] brand,double[] size,double[] price,int[] count){
  80.         System.out.println("----------商场库存清单----------");
  81.         System.out.println("品牌型号 尺寸 价格 库存数");
  82.         //定义变量,计算总库存数,和总价格
  83.         int totalCount = 0;
  84.         int totalMoney = 0;
  85.         //遍历数组,将数组中所有的商品信息打印出来
  86.         for(int i = 0 ; i < brand.length ; i++){
  87.             System.out.println(brand[i]+" "+size[i]+" "+price[i]+" "+count[i]);
  88.             totalCount += count[i];
  89.             totalMoney += count[i]*price[i];
  90.         }
  91.         System.out.println("总库存数: "+totalCount);
  92.         System.out.println("商品库存总金额: "+totalMoney);
  93.     }
  94.     
  95.     /*
  96.      定义方法,实现用户的选择功能,功能的需要返回来
  97.      返回值, int
  98.      参数, 没有
  99.     */
  100.     public static int chooseFunction(){
  101.         System.out.println("-------------库存管理------------");
  102.         System.out.println("1.查看库存清单");
  103.         System.out.println("2.修改商品库存数量");
  104.         System.out.println("3.退出");
  105.         System.out.println("请输入要执行的操作序号:");
  106.         //接受键盘输入
  107.         Scanner sc = new Scanner(System.in);
  108.         int chooseNumber = sc.nextInt();
  109.         return chooseNumber;
  110.     }
  111. }

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI