zoukankan      html  css  js  c++  java
  • 第六次上机作业

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

    import java.util.Scanner;
    
    public class first {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
           int[] arr=new int[5];
            for (int i = 0; i <=arr.length-1; i++) {
                arr[i]=sc.nextInt();
            }
            for (int i : arr) {
                System.out.println(i);
            }
            for (int j = 0; j < arr.length - 1; j++) {
                for (int k = 0; k <arr.length-1-j; k++) {
                    if(arr[k]>arr[k+1]) {
                        int a;
                        a=arr[k];
                        arr[k]=arr[k+1];
                        arr[k+1]=a;
                }
            }
        }
            for (int i : arr) {
                System.out.println(i);
            }
    }
    }

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

    import java.util.Scanner;
    
    public class first {
    
        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,值自己设定)的值。

    import java.util.Scanner;
    
    public class first {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int[][] a = new int[5][4];
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 4; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            System.out.println("你输入的数组为:");
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 4; j++) {
                    System.out.print(a[i][j]);
                }
                System.out.println();
            }

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

    import java.util.Scanner;
    
    public class first {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int[][] a = new int[5][4];
            int max=0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 4; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[i].length-1; j++) {
                    if(max<a[i][j]){
                        max=a[i][j];
                    }
                }
            }
            System.out.println("最大值是:"+max);
        }
        }
  • 相关阅读:
    WEB-INF目录与META-INF目录的作用
    Spring中的jar包详解
    fatal: Could not read from remote repository.的解决办法
    如何解决failed to push some refs to git
    Spring Boot 集成servlet,发布为可直接运行的war包,方便后续打包为docker镜像。
    使用IntelliJ IDEA,gradle开发Java web应用步骤
    git 命令使用常见问题
    自己动手搭建Git服务器-Gitblit
    windows 安装git
    springMVC 接收数组参数,mybatis 接收数组参数,mybatis批量插入/批量删除案例
  • 原文地址:https://www.cnblogs.com/GEM520/p/12696275.html
Copyright © 2011-2022 走看看