2020.4.12.第五次java作业

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

package com.itheima01;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
         int a[]=new int[5];
          Scanner x=new Scanner(System.in);
          System.out.print("请输入5个数");
          for(int i=0;i<5;i++)
           a[i]=x.nextInt();
         int b;
          for(int i=0;i<4;i++)
           for(int j=0;j<4-i;j++)//
            if(a[j]>a[j+1]){
             b=a[j];
             a[j]=a[j+1];
             a[j+1]=b;
            }
          
          System.out.println("从小到大输出:");  
          for(int i=0;i<5;i++)
           System.out.print(a[i]+" ");
         }
}

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

package com.itheima01;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int[] x={34,22,35,67,45,66,12,33};
        System.out.println("请输入一个数:");
        int a=sc.nextInt();
        for(int i=0;i<x.length;i++){
            if(x[i]==a){
                System.out.println("下标为"+i);
            }else{
                System.out.println("not found");
            }break;
        }
    }
}

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

package com.itheima01;

import java.util.Scanner;

public class HelloWorld {
     public static void main(String[] args) {
          double[][] arr = { 
                  {2.4,1.2,4.1,1.3 },
                  { 4.1,2.5,2.7,9.3,3.6},
            {0.3,9.1,1.3,2.5},
            {6.2,2.1,4.2,2.4},
            {2,33,24,42} 
            };
            for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
            System.out.print(arr[i][j] + "\t");
            }
            System.out.println(" ");
           }
          } 
}

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

package com.itheima01;

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        int[][] arr={
                {23,33,34,45},
                {54,43,33,32},
                {42,24,32,12}
                };
        int max=arr[0][0];
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                if(max<arr[i][j]){
                    max=arr[i][j];
                }
            }
        }
        System.out.println("次二维数组的最大值是:"+max); 
        
    }
}

 

posted @ 2020-04-14 13:52  恶魔薄荷樱玖  阅读(153)  评论(0编辑  收藏  举报