• 2020.4.14 java第四次作业


    1.定义长度为5的整型数组,输入他们的值,用冒泡排序后输出.

    package java_test;
    
    import java.util.Scanner;
    
    public class test_5 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input=new Scanner(System.in);
            System.out.println("请输入数组的值");
            int[]arr = new int[5];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = input.nextInt();
            }
            for (int i = 0; i < arr.length-1; i++) {
                for (int j = 0; j < arr.length-1-i; j++) {
                    if (arr[j]>arr[j+1]) {
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            System.out.println("冒泡排序后输出为:");
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i] + " ");
            }
        }
    
    }

     2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"

    package java_test;
    
    import java.util.Scanner;
    
    public class test_5 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[]a = new int [] {34,22,35,67,45,66,12,33};
            Scanner input=new Scanner(System.in);
            System.out.println("输入想查询的数字");
            int i=input.nextInt();
            int b = 0;
            for (int j = 0; j < a.length; j++) {
                if (a[j]==i) {
                    System.out.println("该数存在于数组中下标为" +j);
                    b=1;
                }
            }
            if (b==0) {
                System.out.println("not found");
            }
        }
    
    }

    3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。

    package java_test;
    
    import java.util.Scanner;
    
    public class test_5 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            double[][]arr={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4},{5,5,5,5}};
            for(int i=0;i<arr.length;i++){
               System.out.println();
               for(int j=0;j<arr[i].length;j++){
                   System.out.print(arr[i][j]+" ");
               }
            }
        }
    
    }

     4..定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.

    package java_test;
    
    import java.util.Scanner;
    
    public class test_5 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
              int[][]arr={{20,30,40,45},{89,56,12,50},{44,33,22,11}};
              int max=arr[0][0];
              for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i][j]>max) {
                        max=arr[i][j];
                    }
                }
            }
              System.out.println("其中的最大值为:" + max);
        }
    
    }

     

    package java_test;
    import java.util.Scanner;
    public class test_5 {
    public static void main(String[] args) {// TODO Auto-generated method stub/*Scanner input=new Scanner(System.in);System.out.println("请输入数组的值");int[]arr = new int[5];for (int i = 0; i < arr.length; i++) {arr[i] = input.nextInt();}for (int i = 0; i < arr.length-1; i++) {for (int j = 0; j < arr.length-1-i; j++) {if (arr[j]>arr[j+1]) {int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}System.out.println("冒泡排序后输出为:");for (int i = 0; i < arr.length; i++) {System.out.println(arr[i] + " ");}int[]a = new int [] {34,22,35,67,45,66,12,33};Scanner input=new Scanner(System.in);System.out.println("输入想查询的数字");int i=input.nextInt();int b = 0;for (int j = 0; j < a.length; j++) {if (a[j]==i) {System.out.println("该数存在于数组中下标为" +j);b=1;}}if (b==0) {System.out.println("not found");}double[][]arr={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4},{5,5,5,5}};        for(int i=0;i<arr.length;i++){           System.out.println();           for(int j=0;j<arr[i].length;j++){           System.out.print(arr[i][j]+" ");           }        }*/  int[][]arr={{20,30,40,45},{89,56,12,50},{44,33,22,11}};          int max=arr[0][0];          for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr.length; j++) {if (arr[i][j]>max) {max=arr[i][j];}}}          System.out.println("其中的最大值为:"max);}
    }

  • 相关阅读:
    leecode 240. 搜索二维矩阵 II
    leecode 103. 二叉树的锯齿形层序遍历
    leecode 362. 敲击计数器
    leecode 152. 乘积最大子数组
    leecode 560. 和为K的子数组
    面试题 08.12. 八皇后
    leecode 450. 删除二叉搜索树中的节点
    leecode 384. 打乱数组
    leecoode 138. 复制带随机指针的链表
    mysql基本指令2
  • 原文地址:https://www.cnblogs.com/guoyongqi-blog/p/12701131.html
Copyright © 2020-2023  润新知