数组排序练习

int[] u = {2, 3,5, 1, 7, 4};
    for (int j = 0; j u[i+1]){
                int num=u[i];
                u[i]=u[i+1];
                u[i+1]=num;
            }
        }
    }
    for (int i = 0; i < u.length; i++) {
        System.out.print(u[i]+" ");
    }


    System.out.println("----------------------------") ;
    for (int i = 0; i u[i+1]){
            System.out.println("前一位("+u[i]+")比后一位"+u[i+1]+"大,需要交换位置");
            int num=u[i];
            u[i]=u[i+1];
            u[i+1]=num;
        }else{
            System.out.println("前一位("+u[i]+")比后一位"+u[i+1]+"小,不需要交换位置");
        }
    }
    System.out.println("----------------------------") ;
    for (int i = 0; i u[i+1]){
            System.out.println("前一位("+u[i]+")比后一位"+u[i+1]+"大,需要交换位置");
            int num=u[i];
            u[i]=u[i+1];
            u[i+1]=num;
        }else{
            System.out.println("前一位("+u[i]+")比后一位"+u[i+1]+"小,不需要交换位置");
        }
    }



    int temp = u[0];
    u[0]=u[1];
    u[1]=temp;
    for (int i = 0; i < u.length; i++) {
        System.out.print(u[i]+" ");
    }

    for(int i=1;i<=9;i++){
        for(int j=1;j<=i;j++){
                System.out.print(""+i+"*"+j+"="+(i*j)+"\t");
        }
        System.out.println(" ");
    }
    

    int o=0;
    for(int i=1;i

 

public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    //初始化一个员工年龄的数组
    int[] emp=initEmploy(scanner);

    while (true){
        //功能面板
        System.out.println("请选择以下功能:");
        System.out.println("1.给员工年龄排序 2.找到最大年龄 3.找到最小年龄 " +
                "4.新增员工年龄 5.顺序插入一个新员工 6.查找第几个员工的年龄 7.删除员工");
        int function=scanner.nextInt();

        switch (function){
            //给员工年龄排序
            case 1:
                //调用冒泡排序的方法,对数组进行排序
                 sortEmploy(emp);
                break;
            //找出年龄最大的员工
            case 2:
                 findMaxEmploy(emp);
                 break;
            //找出年龄最小的员工
            case 3:
                findMinEmploy(emp);
                break;
            //插入一个新员工
            case 4:
                insertEmploy(emp,scanner);
                break;
            case 5:
                emp=sortInsertEmploy(emp,scanner);
                break;
            case 6:
                selectEmploy(emp,scanner);
                break;
            case 7:
                delectEmploy(emp,scanner);
                break;
            default:
                System.exit(0);
                break;
        }
    }
}

//数组打印的方法
public static void my(int[] arrays,String content){
    System.out.println(content+":");
    for (int i = 0; i < arrays.length; i++) {
        System.out.print(arrays[i]+" ");
    }
}

//初始化一个员工年龄的数组
public static int[] initEmploy(Scanner scanner){
    //功能一:输入员工数量,并初始化一个员工年龄的数组
    System.out.print("请输入员工的数量:");
    int input=scanner.nextInt();
    //通过数量初始化一个数组
    int[] emp=new int[input];
    for (int i = 0; i < emp.length; i++) {
        System.out.println("请输入"+(i+1)+"个员工的年龄:");
        emp[i]=scanner.nextInt();
    }
    return emp;
}


//增加一个员工
public static int[] insertEmploy(int[] emp, Scanner scanner){
    System.out.println("请输入新人职员工的年龄");
    int newAge =scanner.nextInt();
    int[] tempEmployAge=new int[emp.length+1];
    for (int i = 0; i < emp.length; i++) {
        tempEmployAge[i]=emp[i];
    }
    tempEmployAge[tempEmployAge.length-1]=newAge;
    //重点思考的地方
    my(tempEmployAge,"插入新员工的数组");
    return tempEmployAge;
}
//修改一个员工
public static void updataEmploy(int[] emp,Scanner scanner){
    System.out.println("请输入你要修改员工的编号");
    int index =scanner.nextInt();
    System.out.println("请输入你要修改的年龄");
    int age =scanner.nextInt();
    if(index < 1 || index > emp.length+1){
        System.out.println("没有这个员工");
        return;
    }
    emp[index-1]=age;
    System.out.println("你成功将["+index+"]号的员工年龄修改为:"+age);
}
//删除一个员工
public static int[] delectEmploy(int[] emp,Scanner scanner){
    System.out.println("请输入你要删除的员工的编号");
    int index =scanner.nextInt();
    if(index < 1 || index > emp.length){
        System.out.println("没有这个员工");
        return null;
    }
    index--;
    for (int i = index; i < emp.length-1; i++) {
        emp[i]=emp[i+1];
    }
    int[] newEmploys = new int[emp.length-1];
    for (int i = 0; i < newEmploys.length; i++) {
        newEmploys[i]=emp[i];
    }
    System.out.println("你成功将["+index+"]号的员工删除!");
    return newEmploys;
}
//查找一个员工
public static void selectEmploy(int[] emp,Scanner scanner){
    System.out.println("请输入员工的编号");
    int index =scanner.nextInt();
    if(index < 1 || index > emp.length+1){
        System.out.println("没有这个员工");
        return;
    }
    System.out.println("编号为["+index+"]的员工年龄为"+emp[index-1]);
}

//给员工按年龄排个序
public static void sortEmploy(int[] emp){
    for (int j = 0; j < emp.length-1; j++) {
        for (int i = 0; i < emp.length - 1 - j; i++) {
            if (emp[i] > emp[i + 1]) {
               int num=emp[i];
                emp[i]=emp[i+1];
                emp[i+1]=num;
            }
        }
    }
    my(emp,"排序");
}

//查询最大年龄的员工
public static void findMaxEmploy(int[] emp){
    int maxIndex= 0;
    for (int i = 0; i < emp.length; i++) {
        if(emp[i]>emp[maxIndex]){
            maxIndex=i;
        }
    }
    System.out.println("最大的员工年龄是:"+emp[maxIndex]+",他的编号是:"+(maxIndex+1));
}
//查询最小年龄的员工
public static void findMinEmploy(int[] emp){
    int maxIndex= 0;
    for (int i = 0; i < emp.length; i++) {
        if(emp[i]

 

//初始化
public static String[][] goodsName={{"香皂","洗发水","洗脸盆"},{"猪肉","芹菜","干脆面"},{"盐","糖","酱油"}};
public static int[][] goodsPrice={{3,25,15},{20,5,1},{2,8,10}};
public static int[][] buyNumber={{0,0,0},{0,0,0},{0,0,0}};
public static Scanner scanner =new Scanner(System.in);
public static int totalMoney = 0;

public static void main(String[] args) {

    //初始化商店
    //上架商品,给商品定价, (确定商品数量,自行添加,购买的时候可以提示有没有货物)

    //功能面板
    while (true) {
    System.out.println("----------------------欢迎来到欣知商城----------------------");
    System.out.println("请选择");
    System.out.println("1.日用品 2.食品 3.调味品 4.查看购物车 5.付款 6.退出");

    int find = scanner.nextInt();
    switch (find){
        case 1:
        case 2:
        case 3:
            buyNecessities(find);
            break;
        case 4:
            cartView();
            break;
        case 5:
            parMoney();
            break;
            default:
                System.exit(0);
             break;
    }
    }
}

private static void parMoney() {
    cartView();
    System.out.println("你购买的商品总价为:"+totalMoney+"元。");
    //支付的总额
    //这是一种测试的方法,哪里觉得有问题,就在哪里打印一下
    int payTotal = 0;
    while (payTotal < totalMoney){
        System.out.println("请输入你支付的钱数");
        int pay=scanner.nextInt();
        payTotal+=pay;
    }
}


//查看购物车
private static void cartView() {
    //标志一下购物车是否有东西
    boolean cartIfEmpty=true;

    //循环遍历查看已经购买的商品
    for (int i = 0; i < buyNumber.length; i++) {
        for (int j = 0; j < buyNumber[i].length; j++) {
            if(buyNumber[i][j]>0){
                cartIfEmpty=false;//只要有购买的东西,我们就认为购物车不空
                System.out.println("商品名称 "+goodsName[i][j]+"\t" +
                        "数量:"+buyNumber[i][j]+"总价"+(buyNumber[i][j]*goodsPrice[i][j]));
            }
        }
    }
       if(cartIfEmpty){
           System.out.println("空空如也");
       }
}

//购买商品
public static void buyNecessities(int function){
    //下标等于序号减一
    int index = function-1;
    boolean flag = true;
    while (flag){
    System.out.println("---请选择购买的商品---");
    String[] necessities = goodsName[index];
    int[] necessitiesPrice = goodsPrice[index];
    for (int i = 0; i < necessities.length; i++) {
        System.out.print((i+1) + ":" + necessities[i]+" "+necessitiesPrice[i]+"元"+" \t");
    }
        System.out.println(" ");
    //选择商品编号
    int goodIndex=scanner.nextInt();

    //输入数量
    System.out.println("--请输入购买的数量--");
    int goodNumber=scanner.nextInt();

    //每次购买完成之后要将数量累加
    buyNumber[index][goodIndex-1]=goodNumber;
    //同时把这部分的钱累加起来
    totalMoney = goodNumber*goodsPrice[index][goodIndex-1];

    System.out.println("你成功购买了"+goodNumber+"个"+necessities[goodIndex-1]+",\t"+
            "共消费:"+goodNumber*goodsPrice[index][goodIndex-1]+"元");
    //购买成功后的返回功能
    System.out.println("输入1:返回主菜单,输入2:返回上一级菜单,输入3:结束购物并付款");
    int fun=scanner.nextInt();
    if (fun==1){
         flag=false;
    }else if(fun==2){
        flag=true;
    }else if(fun==3){
        flag=true;
    }

    }

 

你可能感兴趣的:(数组排序练习)