1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
package one; import java.util.Scanner; public class one1 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("输入数组的值:"); int[] a= new int[5]; for(int i=0;i<a.length;i++) { a[i]=input.nextInt(); } 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 t=a[j]; a[j]= a[j+1]; a[j+1] = t; } } } System.out.println("冒号排序为:"); 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 one; import java.util.Scanner; public class one1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); boolean flag= false; int[] b = {34,22,35,67,45,66,12,33}; System.out.println("请输入一个数:"); int a = input.nextInt(); for (int i = 0; i < b.length; i++) { if (b[i] == a) { System.out.println("该数的下标是:" + i); flag = true; } } if(flag == false){ System.out.println("not found"); } } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
package one; import java.util.Scanner; public class one1 { public static void main(String[] args) { double[][] a = { { 1.3,2.3,4.5,1.7,5.6 }, { 2.3,4.2,3.5,7.5,6.7 }, { 1.1,9.7,6.8,6.6,5.9 }, { 2.8,34.5,78.6,56.4,33.5 }, { 45.4,66.6,77.7,88.8,99.9 } }; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length - 1; j++) { System.out.print(a[i][j] + " "); } System.out.println(" "); } } }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
package one; import java.util.Scanner; public class one1 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入数组的值:"); int [][] b=new int[3][4]; for(int i=0;i<b.length;i++) { for(int j=0;j<b[i].length;j++) { b[i][j]=input.nextInt(); } } int max=0; for(int i=0;i<b.length;i++) { for(int j=0;j<b[i].length;j++) { if(max<b[i][j]) { max=b[i][j]; } } } System.out.println("最大值为"+max); } }