1. 定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
1 package lit; 2 import java.util.Scanner; 3 public class next1 { 4 5 public static void main(String[] args){ 6 // TODO Auto-generated method stub 7 int[] a=new int[5]; 8 Scanner input=new Scanner(System.in); 9 System.out.println("输入五个数:"); 10 for(int i=0;i<a.length;i++) 11 a[i]=input.nextInt(); 12 for (int i = 0; i < a.length-1; i++) { 13 for (int j = 0; j < a.length-1-i; j++) { 14 int x = 0; 15 if(a[j] > a[j+1]){ 16 x = a[j]; 17 a[j]= a[j+1]; 18 a[j+1] = x; 19 } 20 } 21 } 22 System.out.println("排序为:"); 23 for(int i=0;i<a.length;i++) { 24 System.out.println(a[i]+""); 25 } 26 } 27 }
2. 定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"
1 package lit; 2 import java.util.Scanner; 3 public class next1 { 4 5 public static void main(String[] args){ 6 // TODO Auto-generated method stub 7 Scanner input=new Scanner(System.in); 8 int [] a= {34,22,35,67,45,66,12,33}; 9 System.out.println("输入a: "); 10 boolean x=false; 11 int b=input.nextInt(); 12 for(int i=0;i<a.length;i++) { 13 if(a[i]==b) { 14 System.out.println("在数组中存在"+"下标为: "+i); 15 x=true; 16 } 17 } 18 if(x==false){ 19 System.out.println("not found"); 20 21 } 22 } 23 24 }
3. 以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
1 package text; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 double[][] a = {{1,2,3,4,5},{5,4,3,2,1},{0,9,8,7,6},{6,7,8,9,10},{16,17,18,19,20}}; 7 for(int i=0;i<4;i++) { 8 for(int j=0;j<5;j++) { 9 System.out.print(a[i][j]+" "); 10 } 11 System.out.println(); 12 } 13 } 14 }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
1 package text; 2 3 public class ZuoYe { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int [][]a={{37,26,52,9},{13,25,1,8},{74,28,47,65}}; 7 int max=0; 8 for(int i=0;i<a.length;i++){ 9 for(int j=0;j<a[i].length;j++){ 10 if(a[i][j]>max){ 11 max=a[i][j]; 12 } 13 } 14 15 } 16 System.out.println(max); 17 } 18 }