升序输出
package kgy;
public class text02 {
public static void main(String[] args) {
int[] arr= {0,5,2,9,4,1,6,7,8,3};
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
for(int i=1;i<arr.length;i++) {
for(int j=0;j<arr.length-1;j++) {
if(arr[j]>arr[j+1]) {
int k=arr[j];
arr[j]=arr[j+1];
arr[j+1]=k;
}
}
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
package kdjsc;
import java.util.*;
public class mpf {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int[] arr=new int[5];
for(int i=0;i<arr.length;i++) {
System.out.print("请输入5位同学的成绩:");
arr[i]=input.nextInt();
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
for(int i=1;i<arr.length;i++) {
for(int j=0;j<arr.length-1;j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
}
逆序输出
public class java004 {
public static void main(String[] args) {
int[] a= {2,5,7,4,1};
int i;
for(i=a.length-1;i>=0;i--) {
System.out.print(a[i]+" ");
}
}
}
public class java02 {
public static void main(String[] args) {
int[] a= {2,5,7,4,1};
int i,temp;
for(i=0;i<a.length/2;i++) {
temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
for(i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
}
}