1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出. package demo3; import java.util.Scanner; public class day01 { public static void main(String[] args) { int[] arr={32,12,94,73,21}; for (int x = 0; x < arr.length; x++) { for (int y = 0; y < arr.length-x-1; y++) { if (arr[y]>arr[y+1]) { int temp=arr[y]; arr[y]=arr[y+1]; arr[y+1]=temp; } } } for (int i= 0; i< arr.length; i++) { System.out.print(arr[i]+" "); } } }
2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found" package demo3; import java.util.Scanner; public class day01 { public static void main(String[] args) { int[] arr={34,22,35,67,45,66,12,33}; Scanner input=new Scanner(System.in); System.out.print("请输入一个数a:"); int a=input.nextInt(); for (int x = 0; x < arr.length; x++) { if (a==arr[x]) { System.out.println("下标为"+x); break; } else{ System.out.println("not found"); } } } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。 package demo3; import java.util.Scanner; public class day01 { public static void main(String[] args) { double[][] arr={{3.4,2.2,3.5,6.7},{4.5,6.6,1.2,3.3},{2.1,4,3,9.1},{1,2,23,1},{2,3,4,3}}; for (int x = 0; x < 5; x++) { for (int i = 0; i < 4; i++) { System.out.print(arr[x][i]+" "); } System.out.println(); } } }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值. package demo3; public class day01 { public static void main(String[] args) { int arr[][]={{10,2,30},{43,52,26}, {77,18,39},{10,1,42}}; int max=arr[0][0]; for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ if(arr[i][j]>max){ max=arr[i][j]; } } } System.out.println("数组的最大值为"+max); } }