1定义数组冒泡排序
package ffff; import java.util.*; public class gggg { public static void main(String[] args) { // TODO Auto-generated method stub int []a=new int[5]; Scanner input=new Scanner(System.in); for (int i = 0; i < a.length; i++) { int x=input.nextInt(); a[i]=x; } 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; } } } for (int i = 0; i < a.length; i++) { System.out.println(a[i]+""); } } }
2查找
package ffff; import java.util.*; public class gggg { public static void main(String[] args) { // TODO Auto-generated method stub int []a={34,22,35,67,45,66,12,33}; int c=0; Scanner input=new Scanner(System.in); System.out.println("输入一个数"); int x=input.nextInt(); for (int i = 0; i < a.length; i++) { if(x==a[i]){ c=1; System.out.println(i); } } if(c==0){ System.out.println("no"); } } }
3数组
package ffff; public class gggg { public static void main(String[] args) { // TODO Auto-generated method stub double[][]a={{1,2,3,4},{5,6,7,8},{56,44,74,88},{5,65,23,11},{45,55,85,42}}; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { System.out.print(a[i][j]+""); } System.out.println(); } } }
最大值
package ffff; public class gggg { public static void main(String[] args) { // TODO Auto-generated method stub int[][]a={{1,2,3,4},{5,6,7,8},{56,44,74,88}}; 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); } }