1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
package d01; public class text { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 4, 5, 2, 3, 1 }; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { int c = a[j]; a[j] = a[j + 1]; a[j + 1] = c; } } } for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"
package d01; import java.util.Scanner; //2.定义数组{34,22,35,67,45,66,12,33}, //输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found" public class text1 { public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 34, 22, 35, 67, 45, 66, 12, 33 }; boolean flag=false; Scanner x = new Scanner(System.in); System.out.println("请输入"); int b = x.nextInt(); for (int i = 0; i < a.length; i++) { if (a[i] == b) { System.out.println("下标为" + i); flag= true; } } if (flag == false) { System.out.println("not found"); } } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
package d01; //3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。 public class text2 { public static void main(String[] args) { // TODO Auto-generated method stub double[][] a={{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4},{5,5,5,5}}; 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,值自己设定),求该二维数组的最大值.
package d01; //4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值. public class text3 { public static void main(String[] args) { // TODO Auto-generated method stub int[][] a = { { 7, 8, 5 }, { 9, 4, 6 }, { 3, 2, 1 } }; int max = a[0][0]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (a[i][j] > a[0][0]) { max = a[i][j]; } } } System.out.println("最大值为"+max); } }