• 第六周作业


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

    package learn;
    import java.util.Arrays;
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method study
                    int[] aa = {1,6,2,3,9};
                    //冒泡排序
                    for(int i=0; i<aa.length-1; i++) {  
                        for(int y=0; y<aa.length-i-1; y++) {  
                            if(aa[y]>aa[y+1]) {
                                int temp = aa[y];
                                aa[y] = aa[y+1];
                                aa[y+1] = temp;
                            }
                        }
                    }
                    for(int element : aa) {
                        System.out.print(element+" ");
                    }
                }
    }

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

    package learn;
    import java.util.Scanner;
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] arr= { 34, 22, 35, 67, 45, 66, 12, 33 };
            Scanner sc = new Scanner(System.in);
            System.out.println("输入数字,查找它是否在数组中");
            int x = sc.nextInt();
            int y = 0;
            for (int i = 0; i < arr.length; i++) {
                if (x == arr[i]) {
                    System.out.println("下标为:" + i);
                    y=1;
                    }
                }
            if (y == 0) {
            System.out.println("not found");
            }
        }    
    }

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

    package learn;
    import java.util.Scanner;
    public class learn1 {
        public static void main(String[] args){
            double[][] arr = { { 1,2,3,4 }, { 5,4,2,1 },
                    { 6,7,8,9 }, { 9,10,13,12}, { 14,15,16,17  } };
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    System.out.print(arr[i][j] + "	");
                }
                System.out.println();
            }
        }
    }
    
        

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

    package learn;
    public class learn1 {
        public static void main(String[] args){
            int arr[][] = { { 0,9,8 }, { 7,6,5 }, { 4,3,2 }, { 2,12,1 } };
            int max = arr[0][0];
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    if (arr[i][j] > max) {
                        max = arr[i][j];
                            }
                        }
                    }
                    System.out.println("该二维数组的最大值" + max);
                }
    }

  • 相关阅读:
    Intent
    What should we do next in general after collecting relevant data
    NOTE FOR Secure Friend Discovery in Mobile Social Networks
    missing pcap.h
    after building Android Source code
    plot point(one column)
    When talking to someone else, don't infer that is has been talked with others at first. It may bring repulsion to the person who is talking with you.
    进程基本知识
    Python input和raw_input的区别
    强制 code review:reviewboard+svn 的方案
  • 原文地址:https://www.cnblogs.com/zhangbowen123/p/12692150.html
Copyright © 2020-2023  润新知