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

    
    
    
    
    1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
    package demo3;
    
    import java.util.Scanner;
    
    public class day01 {
    
        public static void main(String[] args) {
            int[] arr={32,12,94,73,21};
            for (int x = 0; x < arr.length; x++) {
                for (int y = 0; y < arr.length-x-1; y++) {
                    if (arr[y]>arr[y+1])
                    {
                        int temp=arr[y];
                        arr[y]=arr[y+1];
                        arr[y+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 demo3;
    
    import java.util.Scanner;
    
    public class day01 {
    
        public static void main(String[] args) {
            int[] arr={34,22,35,67,45,66,12,33};
            Scanner input=new Scanner(System.in);
            System.out.print("请输入一个数a:");
            int a=input.nextInt();
            for (int x = 0; x < arr.length; x++) {
                if (a==arr[x])
                    {
                        System.out.println("下标为"+x);
                        break;
                    }
                else{
                    System.out.println("not found");
                }
                }
          }
    
        }

    3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
    package demo3;
    
    import java.util.Scanner;
    
    public class day01 {
    
        public static void main(String[] args) {
            double[][] arr={{3.4,2.2,3.5,6.7},{4.5,6.6,1.2,3.3},{2.1,4,3,9.1},{1,2,23,1},{2,3,4,3}};
            for (int x = 0; x < 5; x++) {
                for (int i = 0; i < 4; i++) {
                    System.out.print(arr[x][i]+"	");
                }
                System.out.println();
            }
        }
     }

    4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
    package demo3;
    public class day01
    {
        public static void main(String[] args) 
    {
            int arr[][]={{10,2,30},{43,52,26},
                    {77,18,39},{10,1,42}};
            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);
       }
    }

  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/108-com/p/12701004.html
Copyright © 2011-2022 走看看