69期-Java SE-005_二维数组综合练习-001-002

Posted hijackykun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了69期-Java SE-005_二维数组综合练习-001-002相关的知识,希望对你有一定的参考价值。

 

Test.java

public class Test 
    public static void main(String[] args) 
        int[] array = 73,80,62,93,96,87;
        //求最大值
        int max = array[0];
        for(int i=1;i<array.length;i++) 
            if(array[i]>max) 
                max = array[i];
            
        
        System.out.println("数组中的最大值是"+max);
        //求最小值
        int min = array[0];
        for(int i=1;i<array.length;i++) 
            if(array[i]<min) 
                min = array[i];
            
        
        System.out.println("数组中的最小值是"+min);
    

 

Test2.java

public class Test2 
    public static void main(String[] args) 
        int[] array = 96,93,87,80,73,62;
        System.out.println("添加之前的数组");
        for(int i=0;i<array.length;i++) 
            System.out.print(array[i]+" ");
        
        //将83插入到下标为3的位置
        //96,93,87,83,80,73,62
        //创建新的数组
        int[] array2 = new int[array.length+1];
        for(int i=0;i<3;i++) 
            array2[i] = array[i];
        
        for(int j=4;j<array2.length;j++) 
            array2[j] = array[j-1];
        
        array2[3] = 83;
        System.out.println("");
        System.out.println("添加之后的数组");
        for(int i=0;i<array2.length;i++) 
            System.out.print(array2[i]+" ");
        
    

 

Test3.java

import java.util.Arrays;

public class Test3 
    public static void main(String[] args) 
        int[] array = 73,80,62,93,96,87;
        for(int i=0;i<array.length-1;i++) 
            for(int j=0;j<array.length-1-i;j++) 
                if(array[j]<array[j+1]) 
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                
            
        
        System.out.println(Arrays.toString(array));
    

 

Test4.java

import java.util.Arrays;

public class Test4 
    public static void main(String[] args) 
        int[] array = 73,80,62,93,96,87;
        int[] array2 = 73,80,62,93,96,87;
        int[] array3 = 66,55,44,33,22;
        boolean flag = Arrays.equals(array, array3);
        if(flag == true) 
            System.out.println("两个数组相等");
        else 
            System.out.println("两个数组不相等");
        
        System.out.println(Arrays.toString(array));
//        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        Arrays.fill(array2, 66);
        System.out.println(Arrays.toString(array2));
        int[] copyArray = Arrays.copyOf(array3, 3);
        System.out.println(Arrays.toString(copyArray));
        int index = Arrays.binarySearch(array, 62);
        System.out.println(index);
    

 

Test5.java

public class Test5 
    public static void main(String[] args) 
        //声明二维数组
        int[][] array;
        //开辟内存空间
        array = new int[2][5];
        //赋值
        array[0][0] = 50;
        array[0][1] = 60;
        array[0][2] = 70;
        array[0][3] = 80;
        array[0][4] = 90;
        array[1][0] = 150;
        array[1][1] = 160;
        array[1][2] = 170;
        array[1][3] = 180;
        array[1][4] = 190;
        int[][] array2 = 1,2,3,4,5,6;
        int[][] array3 = new int[][] 1,2,3,4,5,6;
        System.out.println(array3[1][1]);
        
    

 

Test6.java

public class Test6 
    public static void main(String[] args) 
        int[][] array = new int[3][6];
        int sum = 0;
        for(int i=0;i<array.length;i++) 
            System.out.println("------ 第"+(i+1)+"层货架 ------");
            for(int j=0;j<array[i].length;j++) 
                int num = (i+1)*(j+1);
                System.out.println("第"+(j+1)+"个格挡的鸡蛋个数:"+num);
                sum += num;
            
        
        System.out.println("鸡蛋的总数是:"+sum);
    

 

Test7.java

import java.util.Arrays;
import java.util.Scanner;

public class Test7 
    public static void main(String[] args) 
        //初始化用户名称
        String[] nameArray = "张三","李四","王五","小明";
        //初始化用户年龄
        int[] ageArray = 22,23,20,22;
        //初始化用户状态
        String[] stateArray = "正常","正常","正常","正常";
        Scanner scanner = new Scanner(System.in);
        int num;
        String name;
        int age;
        do 
            System.out.println("欢迎使用用户管理系统");
            System.out.println("1.查询用户");
            System.out.println("2.添加用户");
            System.out.println("3.删除用户");
            System.out.println("4.账号冻结");
            System.out.println("5.账号解封");
            System.out.println("6.退出系统");
            System.out.print("请选择:");
            num = scanner.nextInt();
            switch(num) 
            case 1:
                System.out.println("------ 查询用户 ------");
                System.out.println("编号\t\t姓名\t\t年龄\t\t状态");
                for(int i=0;i<nameArray.length;i++) 
                    System.out.println((i+1)+"\t\t"+nameArray[i]+"\t\t"+ageArray[i]+"\t\t"+stateArray[i]);
                
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 2:
                System.out.println("------ 添加用户 ------");
                System.out.print("请输入用户姓名:");
                name = scanner.next();
                System.out.print("请输入用户年龄:");
                age = scanner.nextInt();
                nameArray = Arrays.copyOf(nameArray, nameArray.length+1);
                nameArray[nameArray.length-1] = name;
                ageArray = Arrays.copyOf(ageArray, ageArray.length+1);
                ageArray[ageArray.length-1] = age;
                stateArray = Arrays.copyOf(stateArray, stateArray.length+1);
                stateArray[stateArray.length-1] = "正常";
                System.out.println("添加成功!");
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 3:
                System.out.println("执行删除用户的功能");
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 4:
                System.out.println("执行账号冻结的功能");
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 5:
                System.out.println("执行账号解封的功能");
                System.out.print("输入0返回:");
                num = scanner.nextInt();
                break;
            case 6:
                System.out.println("感谢使用用户管理系统!");
                break;
            
        while(num == 0);
    

 

以上是关于69期-Java SE-005_二维数组综合练习-001-002的主要内容,如果未能解决你的问题,请参考以下文章

69期-Java SE-035_MySQL-6 存储过程SQL练习 -doing

69期-Java SE-004_循环数组-001-002

69期-Java SE-045_JSP-2

69期-Java SE-008_面向对象-2-001-002

69期-Java SE-007_面向对象-1-001-002

69期-Java SE-015_集合-1-001-002