4.10
//1.定义长度位5的整型数组,输入他们的值用冒泡排序后输出. package learn; public class day06 { public static void main(String[] args) { int[] a = new int[] { 1, 3, 2, 5, 8 }; int t; for (int i = 0; i < a.length-1; i++) { for (int j = 0; j < a.length-1-i;j++) { if (a[j + 1] > a[j]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for (int j = 0; j < a.length; j++) { System.out.print(a[j] + ", "); } } }
//2定义数组{34,22,35,67 ,45,66,12,33}输入-个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found" package learn; import java.util.*; public class day06 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] n = new int[] { 34, 22, 35, 67, 45, 66, 12, 33 }; System.out.println("输入一个数"); int a = input.nextInt(); int f=0,i,t=0; for (i = 0; i < n.length; i++) { if (a == n[i]){ f=1; t=i; } } if(f==1){ System.out.println("存在,下标为:" + t); } else{ System.out.println("not found"); } } }
//3.以矩阵的形式输出一-个double型二维数组(长度分别为5、4 ,值自己设定)的值。 package learn; public class day06 { public static void main(String[] args) { double[][] a=new double[5][4]; 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,值自己设定),求该二维数组的最大值. package learn; public class day06 { public static void main(String[] args) { int[][] a={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}; int t=a[0][0]; for(int i=0;i<4;i++){ for(int j=0;j<3;j++){ if(a[i][j]>t){ t=a[i][j]; } } } System.out.println("最大为:"+t); } }