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