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