1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
package afk; import java.util.Scanner; public class Zuoye7 { public static void main(String[] args) { int []a=new int [5]; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { int b=input.nextInt(); a[i]=b; } for (int i = 1; i < a.length; i++) { for (int j = 0; j < a.length-i; j++) { if(a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } 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 afk; import java.util.Scanner; public class Zuoye8 { public static void main(String[] args) { int []a= {34,22,35,67,45,66,12,33}; Scanner input=new Scanner(System.in); System.out.println("请输入一个数:"); int b=input.nextInt(); int xiabiao=-1; for (int i = 0; i < a.length; i++) { if(a[i]==b) { xiabiao=i; System.out.println(i); break; } } if(xiabiao==-1) System.out.println("not found"); } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值
public class Zuoye9 { public static void main(String[] args) { double[][]a={ {5,6,9,4},{8,7,1,6},{89,51,4,25},{4,68,25,41},{12,18,36,28}}; 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,值自己设定),求该二维数组的最大值
public class Zuoye10 { public static void main(String[] args) { int[][]a= {{4,9,41,99},{2,45,29,67},{7,6,48,71}}; int max=0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { if(a[i][j]>max) { max=a[i][j]; } } } System.out.println(max); } }