第六周作业

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

 

package Svd;

import java.util.*;

public class bbt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = new int[5];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.print("输入第" + (i + 1) + "个数:");
            arr[i] = sc.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;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

    }

}

 

 

 

 
   

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

 

package Svd;

import java.util.*;

public class bbt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = { 34, 22, 35, 67, 45, 66, 12, 33 };
        int x = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个数:");
        int a = sc.nextInt();
        for (int i = 0; i < arr.length; i++) {

            if (a == arr[i]) {
                x = 1;
                System.out.println("下标为"+i);
            }

        }
        if (x == 0) {
            System.out.println("no found");
        }

    }
}

 

 

 

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

 

package Svd;

import java.util.*;

public class bbt {

    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++) {
            for (int j = 0; j < arr.length - 1; j++) {
                System.out.print(arr[i][j] + "  ");
            }
            System.out.println("");
        }
    }
}

 

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

 

package Svd;

import java.util.*;

public class bbt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double[][] arr = { { 1.0, 2.0, 3.0, 4.0 }, { 6.0, 7.0, 8.0, 9.0 }, { 12.0, 13.0, 14.0, 15.0 } };
        double max = arr[0][0];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (max < arr[i][j]) {
                    double a;
                    a = max;
                    max = arr[i][j];
                }
            }
        }
        System.out.println("最大值为:" + max);
    }
}

 

 

posted @ 2020-04-14 15:33  计算机18  阅读(169)  评论(0编辑  收藏  举报