JAVA编程:定义一个整数类型的一维数组,通过键盘给数组元素赋值。并写一方法f()实现数组元素从小到大排列

定义一个整数类型的一维数组,通过键盘给数组元素赋值。并写一方法f()实现数组元素从小到大排列,并将结果输出到屏幕。在主方法调用f()方法。

import java.util.Scanner;

public class test1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int[] a=new int[10];
for(int i=0;i<a.length;i++){
System.out.println("请输入"+a.length+"个数字,当前第"+(i+1)+"个:");
a[i]=sc.nextInt();
}
System.out.print("排序前数组: ");
for(int i:a){
System.out.print(i+"\t");
}
System.out.println();
new test1().f(a);
}
public void f(int[] a){
for(int i=a.length-1;i>0;i--){
for(int j=0;j<i;j++){
if(a[i]<a[j]){
int temp=a[i];;
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.print("排序后的数组: ");
for(int i:a){
System.out.print(i+"\t");
}
}
}
温馨提示:答案为网友推荐,仅供参考