代码改变世界

java中方法的使用 重载-0512

2018-05-14 11:40  yelena  阅读(148)  评论(0编辑  收藏  举报
class Demo02 
{
    public static void main(String[] args) 
    {
        System.out.println(add(1,2));
        System.out.println(add(1,2,8));
        System.out.println(add(1.1,2.2));
    }
    public static int add(int a,int b){
           return a+b;
    }
    public static int add(int a,int b,int c){
           return a+b+c;
    }
    public static double add(double a,double b){
           return a+b;
    }
}

 

import java.util.Scanner;
class Demo01
{
    public static void main(String[] args) 
    {
        String[] pinpai = {"MacBookAir","ThinkpadT450","ASUS-FL5800"};
        double[] size = {13.0,14.0,15.6};
        double[] price ={6988.88,5999.99,4999.5};
        int[] count = {5,10,18};
        while(true){
             switch (xz())
             {
             case 1:show(pinpai,size,price,count);
                     break;
             case 2:xg(pinpai,size,price,count);
                     break;
             case 3:
                 return;
             default:
                 System.out.println("输入有误");
                     break;
             
             }
        }
    }
    public static int xz(){
        System.out.println("1、查看商品库存");
        System.out.println("2、修改库存数量");
        System.out.println("3、退出");
        System.out.println("输入操作编号");
        System.out.println("-----------------------------");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        return num;
    }
    

    public static void show (String[] pinpai,double[] size,double[] price,int[] count){
        System.out.println("----------------------商城库存清单--------------------");
        System.out.println("品牌型号\t尺寸\t价格\t库存数");
        int csum = 0;
        for(int i=0;i<pinpai.length;i++){
          System.out.println(pinpai[i]+"\t"+size[i]+"\t"+price[i]+"\t"+count[i]);
          csum = csum + count[i];
        }
        System.out.println("库存数\t"+csum);
        System.out.println("---------------123------------------");
    }



    public static void xg(String[] pinpai,double[] size,double[] price,int[] count){
        System.out.println("商品编号"+"品牌型号\t尺寸\t价格\t库存数");
        for(int i=0;i<pinpai.length;i++){
          System.out.println(i+"\t"+pinpai[i]+"\t"+size[i]+"\t"+price[i]+"\t"+count[i]);
        }
        System.out.println("----------------------------------------------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("输入编号");
        int num = sc.nextInt();
        System.out.println("输入数量");
        int n = sc.nextInt();
        count[num] = n;
    }

}