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

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

    import java.util.*;
    
    public class aa {
    
    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    int[] a = new int [5] ; 
    Scanner sc = new Scanner (System.in);
    System.out.println("请依次输入数组各元素的值");
    for (int i = 0; i < a.length; i++) {
    a[i]=sc.nextInt();
    }
    for (int i=0;i<a.length-1;i++) {
    for(int j=0;j<a.length-i-1;j++) {
    if(a[j]>a[j+1]) {
    int x=a[j];
    a[j]=a[j+1];
    a[j+1]=x;
    }
    }
    }
    for (int i = 0; i < a.length; i++) {
    System.out.print(a[i] + " ");
    }
    }
    }

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

    package a;
    
    import java.util.*;
    
    public class aa {
    
    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    int[] a = { 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 < a.length; i++) {
    if (x == a[i]) {
    System.out.println("它的下标为:" + i);
    y=1;
    }
    }
    if (y == 0) {
    System.out.println("not found");
    }
    }
    }

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

    package a;
    
    import java.util.*;
    
    public class aa {
    
    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    double[][] a = new double [4][5];
    double x=0.1;
    Scanner sc = new Scanner(System.in);
    for(int i=0;i<a.length;i++) {
    for(int j=0;j<a[i].length;j++) {
    a[i][j]+=x;
    x++;
    }
    }
    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 a;
    
    import java.util.*;
    
    public class aa {
    
    public static void main(String[] args) {
    // TODO Auto-generated method stub;
    int[][] a = new int[3][4];
    int x = 1;
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    a[i][j] += x;
    x++;
    }
    }
    System.out.println("输入数组为:");
    int max = 0;
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    System.out.print(a[i][j] + " ");
    if (max < a[i][j]) {
    max = a[i][j];
    }
    }
    System.out.println();
    }
    System.out.println("输出的最大值为:" + max);
    }
    }
  • 相关阅读:
    几个新角色:数据科学家、数据分析师、数据(算法)工程师
    人类投资经理再也无法击败电脑的时代终将到来了...
    Action Results in Web API 2
    Multiple actions were found that match the request in Web Api
    Routing in ASP.NET Web API
    how to create an asp.net web api project in visual studio 2017
    网站漏洞扫描工具
    How does asp.net web api work?
    asp.net web api history and how does it work?
    What is the difference between a web API and a web service?
  • 原文地址:https://www.cnblogs.com/Zzzhqh/p/12704965.html
Copyright © 2011-2022 走看看