第六周作业

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

package com.run.test;

import java.util.Scanner;

public class One {
 
    public static void main(String[] args) {
        int[] num = new int[5];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < num.length; i++) {
            num[i] = input.nextInt();
        }
        for (int i = 0; i < num.length - 1; i++) {
            for (int j = 0; j < num.length - 1 - i; j++) {
                if (num[j] > num[j + 1]) {
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + " ");
        }
 
    }
 
}

 

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

import java.util.Scanner;

public class One {
 
     public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            boolean flag= false;
            int[] b = {34,22,35,67,45,66,12,33};
            System.out.print("请输入一个数:");
            int a = input.nextInt();
            for (int i = 0; i < b.length; i++) {
                if (b[i] == a) {
                    System.out.println("该数的下标是:" + i);
                    flag = true;
                }
            }
                if(flag == false){
                    System.out.println("not found");
                }
        }
 
}

 

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

public class One {
 
    public static void main(String[] args) {

        double[][] a = { { 1.1, 3.2, 5.3, 7.4 }, { 9.1, 11,2, 13.3, 15.4 },
                { 17.1, 19.2, 21.3, 23.4 }, { 25.1, 27.2, 29.3, 31.4 },
                { 33, 35, 37, 39 } };
                for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a.length - 1; j++) {
                System.out.print(a[i][j] + "\t");
                }
                System.out.println(" ");
            }
        } 
 
}

 

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

public class One {
 
    public static void main(String[] args) {
        int[][] arr = {{11,22,33,44},{15,25,35,45},{69,66,99,96}};
        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);
    } 
 
}

 

posted @ 2020-04-16 03:10  风见崎鸣  阅读(68)  评论(0编辑  收藏  举报