溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[JAVA]復(fù)習(xí)第五日JAVA-Shop案例(數(shù)組遍歷練習(xí))

發(fā)布時間:2020-08-11 09:21:46 來源:ITPUB博客 閱讀:165 作者:Taiaishua 欄目:編程語言

點擊(此處)折疊或打開

  1. /*
  2.    實現(xiàn)商品的庫存管理
  3.      功能:
  4.      1. 展示用戶選擇功能清單
  5.         2. 根據(jù)選擇的功能編號,進行不同的操作
  6.          A. 展示所有庫存
  7.          B. 修改庫存數(shù)量
  8.         
  9.      分析:
  10.      1. 展示用戶清單:
  11.          輸出語句, 用戶輸入, 選擇功能序號
  12.         2. 根據(jù)選擇,調(diào)用不同的方法
  13.          switch語句
  14.              case 1 2 3
  15.         
  16.          A 展示庫存
  17.          將存儲商品的數(shù)組,遍歷
  18.          B 修改庫存
  19.         
  20.              修改所有的庫存數(shù)量
  21. */
  22. import java.util.Scanner;
  23. public class Shopp{
  24.     public static void main(String[] args){
  25.         //使用數(shù)組,保存商品的信息
  26.         //品名,尺寸,價格,庫存數(shù), 定義5個數(shù)組
  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.              //調(diào)用查看庫存清單方法
  36.              printStore(brand,size,price,count);
  37.             break;
  38.             
  39.             case 2:
  40.              //調(diào)用修改庫存的方法
  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.         參數(shù), 庫存數(shù)的數(shù)組, 品名數(shù)組
  59.     */
  60.     public static void update(String[] brand, int[] count){
  61.         //遍歷數(shù)組,遍歷到一個,修改一個
  62.         //接受鍵盤輸入
  63.         Scanner sc = new Scanner(System.in);
  64.         //遍歷數(shù)組
  65.         for(int i = 0; i < brand.length ; i++){
  66.             System.out.println("請輸入"+brand[i]+"的庫存數(shù)");
  67.             //鍵盤輸入,錄入庫存, 存儲到庫存的數(shù)組中
  68.             int newCount = sc.nextInt();
  69.             count[i] = newCount;
  70.         }
  71.         //int chooseNumber = sc.nextInt();
  72.     }
  73.     
  74.     /*
  75.      定義方法,展示所有的庫存清單,遍歷
  76.      返回值,沒有
  77.      參數(shù), 數(shù)組
  78.     */
  79.     public static void printStore(String[] brand,double[] size,double[] price,int[] count){
  80.         System.out.println("----------商場庫存清單----------");
  81.         System.out.println("品牌型號 尺寸 價格 庫存數(shù)");
  82.         //定義變量,計算總庫存數(shù),和總價格
  83.         int totalCount = 0;
  84.         int totalMoney = 0;
  85.         //遍歷數(shù)組,將數(shù)組中所有的商品信息打印出來
  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("總庫存數(shù): "+totalCount);
  92.         System.out.println("商品庫存總金額: "+totalMoney);
  93.     }
  94.     
  95.     /*
  96.      定義方法,實現(xiàn)用戶的選擇功能,功能的需要返回來
  97.      返回值, int
  98.      參數(shù), 沒有
  99.     */
  100.     public static int chooseFunction(){
  101.         System.out.println("-------------庫存管理------------");
  102.         System.out.println("1.查看庫存清單");
  103.         System.out.println("2.修改商品庫存數(shù)量");
  104.         System.out.println("3.退出");
  105.         System.out.println("請輸入要執(zhí)行的操作序號:");
  106.         //接受鍵盤輸入
  107.         Scanner sc = new Scanner(System.in);
  108.         int chooseNumber = sc.nextInt();
  109.         return chooseNumber;
  110.     }
  111. }

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI