统计
  • 建站日期:2021-03-10
  • 文章总数:518 篇
  • 评论总数:151 条
  • 分类总数:32 个
  • 最后更新:4月20日
文章 未分类

day_code案例

梦幻书涯
首页 未分类 正文

/*
   实现商品的库存管理
     功能:
     1. 展示用户选择功能清单
  2. 根据选择的功能编号,进行不同的操作
     A. 展示所有库存
     B. 修改库存数量
    
   分析:
     1. 展示用户清单:
     输出语句, 用户输入, 选择功能序号
  2. 根据选择,调用不同的方法
      switch语句
     case 1 2 3
  
     A  展示库存
       将存储商品的数组,遍历
     B  修改库存
         
     修改所有的库存数量
*/
import java.util.Scanner;
public class Shopp{
 public static void main(String[] args){
  //使用数组,保存商品的信息
  //品名,尺寸,价格,库存数, 定义5个数组
  String[] brand = {"MacBookAir","ThinkpadT450"};
  double[] size = {13.3,15.6};
  double[] price = {9998.97,6789.56};
  int[] count = {0,0};
  while(true){
  int choose = chooseFunction();
  switch(choose){
   case 1:
     //调用查看库存清单方法
     printStore(brand,size,price,count);
   break;
   
   case 2:
     //调用修改库存的方法
     update(brand,count);
   break;
   
   case 3:
    return ;
   
   
   default:
     System.out.println("没有这个功能");
   break;
  }
  }
 }
 /*
   定义方法,修改所有商品的库存
     用户输入1个,修改1个
  返回值,没有
  参数, 库存数的数组, 品名数组
 */
 public static void update(String[] brand, int[] count){
  //遍历数组,遍历到一个,修改一个
  //接受键盘输入
  Scanner sc = new Scanner(System.in);
  //遍历数组
  for(int i = 0; i < brand.length ; i++){
   System.out.println("请输入"+brand[i]+"的库存数");
   //键盘输入,录入库存, 存储到库存的数组中
   int newCount = sc.nextInt();
   count[i] = newCount;
  }
  //int chooseNumber = sc.nextInt();
 }
 
 /*
    定义方法,展示所有的库存清单,遍历
    返回值,没有
    参数, 数组
 */
 public static void printStore(String[] brand,double[] size,double[] price,int[] count){
  System.out.println("----------商场库存清单----------");
  System.out.println("品牌型号     尺寸    价格    库存数");
  //定义变量,计算总库存数,和总价格
  int totalCount = 0;
  int totalMoney = 0;
  //遍历数组,将数组中所有的商品信息打印出来
  for(int i = 0 ; i < brand.length ; i++){
   System.out.println(brand[i]+"   "+size[i]+"    "+price[i]+"   "+count[i]);
   totalCount += count[i];
   totalMoney += count[i]*price[i];
  }
  System.out.println("总库存数: "+totalCount);
  System.out.println("商品库存总金额: "+totalMoney);
 }
 
 /*
   定义方法,实现用户的选择功能,功能的需要返回来
   返回值, int
   参数, 没有
 */
 public static int chooseFunction(){
  System.out.println("-------------库存管理------------");
  System.out.println("1.查看库存清单");
  System.out.println("2.修改商品库存数量");
  System.out.println("3.退出");
  System.out.println("请输入要执行的操作序号:");
  //接受键盘输入
  Scanner sc = new Scanner(System.in);
  int chooseNumber = sc.nextInt();
  return chooseNumber;
 }
}


package jva_06;

 

import java.util.Scanner;
public class Shop{
public static void main(String[] args){
  //使用数组,保存商品的信息
  //品名,尺寸,价格,库存数, 定义5个数组
  String[] brand = {"MacBookAir","ThinkpadT450"};
  double[] size = {13.3,15.6};
  double[] price = {9998.97,6789.56};
  int[] count = {0,0};
  while(true){
  int choose = chooseFunction();
  switch(choose){
   case 1:
     //调用查看库存清单方法
     printStore(brand,size,price,count);
   break;
   
   case 2:
     //调用修改库存的方法
     update(brand,count);
   break;
   
   case 3:
    return ;
   
   
   default:
     System.out.println("没有这个功能");
   break;
  }
  }
 }
      public static int chooseFunction()
  {
  
  
   System.out.println("-------------库存管理------------");
  System.out.println("1.查看库存清单");
  System.out.println("2.修改商品库存数量");
  System.out.println("3.退出");
  System.out.println("请输入要执行的操作序号:");
  
  Scanner sc=new Scanner(System.in);
  int choosenumber=sc.nextInt();
  return choosenumber;
  
  }
     public static void printStore(String[] brand,double[] size,double[] price,int[] count){
   int tatolcount=0;
   int tatolmoney=0;
   for(int i=0;i<brand.length;i++){
    System.out.println(brand[i]+"   "+size[i]+"   "+price[i]+"  "+count[i]);
    tatolcount+=count[i];
    tatolmoney+=count[i]*price[i];
    System.out.println("总库存数:"+tatolcount);
    System.out.println("商品库存总金额: "+tatolmoney);
   }
  }
  
     public static void update(String[] brand,int[]count){
     Scanner sc=new Scanner(System.in);
  
     for(int i=0;i<brand.length;i++){
  System.out.println("请你输入"+brand[i]+"的库存数");
  int newnumber=sc.nextInt();
  count[i]+=newnumber;
    
   }
    
     }
   
  /*
   方法重载的注意事项
     1. 参数列表必须不同
  2. 重载和参数变量名无关
  3. 重载和返回值类型无关
  4. 重载和修饰符无关
  技巧: 重载看方法名和参数列表
*/
public class MethodOverLoadDemo_1{
 //method(1l,1)

 
 public static void method(int a,int b){
  
 }
 public static void method(double a,int b){
  
 }
 
 // void method(int a,int b){
  //return 1;
 //}


    

/*
    方法的重载特性 (overload)
 在同一个类中,允许出现同名的方法,只要方法的参数列表不同即可,这样方法就是重载
 参数列表不同: 参数的个数,数据类型,顺序
*/
public class MethodOverLoadDemo{
 public static void main(String[] args){
  //对于重载的调用,根据参数传递进行区分
  //System.out.println();
  double sum = getSum(2.3,3.5);
  System.out.println(sum);
 }
 /*
   对参数求和,利用方法的重载特性
 */
 public static int getSum(int a,int b){
  System.out.println("两个int参数");
  return a+b;
 }
 public static int getSum(int a,int b,int c){
  System.out.println("三个int参数");
  return a+b+c;
 }
 public static double getSum(double a,double b){
  System.out.println("两个double参数");
  return a+b;
 }
 /*
    定义方法,对参数求和
    参数,没规定几个,数据类型
 
 public static int getSum(int a ,int b){
  return a+b;
 }
 
 public static double getSumDouble(double a,double b){
  return a+b;
 }
 public static int getSum3(int a,int b, int c){
  return a+b+c;
 }*/
}
}

/*
   方法,调用中的参数传递问题
     1. 方法参数是基本数据类型
  2. 方法参数是引用类型
     传递的是内存地址!!!
*/
public class MethodDemo_3{
 public static void main(String[] args){
  /*int a = 1;
  int b = 2;
  change(a,b);
  System.out.println(a); //1
  System.out.println(b); // 2
  */
  int[] arr = {1,2,3,4};
  System.out.println(arr[2]); // 3
  change(arr);
  System.out.println(arr[2]); //  100
 }
 
 public static void change(int[] arr){
  arr[2] = 100;
 }
 
 
 public static void change(int a,int b){
  a = a+b;
  b = b+a;
 }
}


/*
   方法定义和使用的注意事项
     1. 方法不能定义在另一个方法的里面
  2. 写错方法名字
  3. 写错了参数列表
  4. 方法返回值是void,方法中可以省略return 不写
      return 下面不能有代码
  5. 方法返回值类型,和return 后面数据类型必须匹配
  6. 方法重复定义问题
  7. 调用方法的时候,返回值是void, 不能写在输出语句中
*/
public class MethodDemo_2{
 public static void main(String[] args){
  int i = print();
  System.out.println( print() );
 }
 public static int print(){
  
  return 1;
    }

}

/*
   方法的定义练习
*/
import java.util.Scanner;
public class MethodDemo_1{
 public static void main(String[] args){
  //printRect();
  //int number = getNumber();
  //System.out.println(getNumber());
  //printRect2(3,5);
  double avg = getAvg(2,2,3);
  System.out.println(avg);
 }

 /*
    定义有返回值有参数方法,如求三个数的平均值
    明确方法计算后的数据类型, 返回值类型 double
    明确方法未知数, 三个未知的整数
 */
 public static double getAvg(double a, double b,double c){
   return (a+b+c)/3;
 }
 
 /*
     定义无返回值有参数方法,如打印指定M行,每行N个*号的矩形
  明确方法计算后结果,控制台输出图形,没有返回值的
  方法中有没有未知数,图形行数,和列数,是未知的, 数据类型整数int
 */
 public static void printRect2(int m,int n){
  for(int i = 0 ; i < m ; i++){
   for(int j = 0 ; j < n ;  j++){
    System.out.print("*");
   }
   System.out.println();
  }
 }

 /*
    定义有返回值无参数方法,如键盘录入得到一个整数
    明确方法计算后结果的数据类型 int
    明确有没有未知数,没
 */
 public static int getNumber(){
  Scanner sc = new Scanner(System.in);
  //int number = sc.nextInt();
  return sc.nextInt();
 }
 
 /*
    定义无返回值无参数方法,如打印3行,每行3个*号的矩形
    为什么没有返回值:
        打印矩形 ,输出效果,不需要将结果返回
     明确未知数: 不需要未知数
 */
 public static void printRect(){
  for(int i = 0 ; i < 3 ; i++){
   for(int j = 0 ; j < 3 ;j++){
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

这篇文章最后更新于2019-3-30,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
day_06code案例
« 上一篇
第14天类、抽象类、接口作为方法返回值类、抽象类、接口作为方法参数Eclipse常用快捷键操作Eclipse文档注释导出帮助文档Eclipse项目的jar包导出与使用jar包辨析何时定义变量为成员变量
下一篇 »

发表评论

HI ! 请登录
注册会员,享受下载全站资源特权。
Array

日历

热门文章