1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
package lucky; import java.util.Scanner; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub 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 temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } }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 lucky; import java.util.Scanner; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); boolean flag = false; int[] x = new int[]{34,22,35,67,45,66,12,33}; System.out.print("请输出一个数:"); int a = sc.nextInt(); for(int i=0;i<x.length;i++){ if(x[i]==a){ System.out.println("该数存在于数组中,下标为:"+i); flag=true; } }if(flag==false){ System.out.println("not found"); } } }
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
package lucky; import java.util.Scanner; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub double [][] a = {{31,63,86,99},{32,54,78,10}, {11,12,13,14},{16,17,18,19}, {22,44,66,88}}; for(int i=0;i<a.length;i++){ System.out.println(); for(int j=0;j<a.length-1;j++){ System.out.print(a[i][j]+" "); } } } }
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
package lucky; import java.util.Scanner; public class happy { public static void main(String[] args) { // TODO Auto-generated method stub int a[][] = {{22,34,55},{88,10,31}, {77,64,27},{61,52,23}}; 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]>max){ max = a[i][j]; } } } System.out.println("该二维数组的最大值为:"+max); } }