zoukankan      html  css  js  c++  java
  • 第五次作业

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

    package calsswork;
    import java.util.Scanner;
    public class text1 {
        public static void main(String[] args) {
            int[] a = {1,2,4,3,5};
            for(int i=0; i<a.length-1; i++) {  
                for(int b=0; b<a.length-i-1; b++) {  
                    if(a[b]>a[b+1]) {
                        int temp = a[b];
                        a[b] = a[b+1];
                        a[b+1] = temp;
                    }
                }
            }
            for(int element : a) {
                System.out.print(element+";");
            }
        }
    }

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

    package calsswork;
    import java.util.Scanner;
    public class text1 {
        public static void main(String[] args) {
            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 calsswork;
    import java.util.Scanner;
    public class text1 {
        public static void main(String[] args) {
                    double[][] a = {{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 < a.length; i++) {
                        for (int j = 0; j < a[i].length; j++) {
                            System.out.print(a[i][j] + "	");
                        }
                        System.out.println();
                    }
                }
            }

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

    package calsswork;
    import java.util.Scanner;
    public class text1 {
        public static void main(String[] args) {
                    int a[][] = {{1,1,1,1}, {2,2,2,2}, {3,3,3,6}};
                    int max = a[0][0];
                    for (int i = 0; i < a.length; i++) {
                        for (int j = 0; j < a[i].length; j++) {
                            if (a[i][j] > max) {
                                max = a[i][j];
                                    }
                                }
                            }
                            System.out.println("最大值=" + max);
                        }
            }

  • 相关阅读:
    ubuntu上安装nginx+mysql+php5-fpm(PHP5
    创建内存盘
    ubuntu上安装apache2+mysql+php5-fpm(PHP5
    Cubieboard Linaro 搭建超节能监控平台
    CubieBoard开发板数据源介绍
    如何在github创建个人主页?
    android studio配置git
    greenDao使用时遇到的坑
    FragmentManager is already executing transactions
    AndroidSchedulers.mainThread()无法切换到主线程,原来是细节问题啊
  • 原文地址:https://www.cnblogs.com/jiming123/p/12692460.html
Copyright © 2011-2022 走看看