import java.util.*;
public class ArrayPaiXu
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("请输入5个整型数字:");
int[] a=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("请输入第"+(i+1)+"位学员的成绩:");
a[i]=input.nextInt();
}
int temp;
for(int j=0;j<a.length;j++)
{
temp=a[j];
a[j]=a[a.length-j-1];
a[a.length-j-1]=temp;
System.out.println(a[j]);
}
}
}
------解决方案--------------------
for(int j=0;j<a.length;j++)
{
temp=a[j];
a[j]=a[a.length-j-1];
a[a.length-j-1]=temp;
System.out.println(a[j]);
}
交换了两次
j = 0时0----4交换
j = 4时4----0交换
------解决方案--------------------
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("请输入5个整型数字:");
int[] a=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("请输入第"+(i+1)+"位学员的成绩:");
a[i]=input.nextInt();
}
int temp;
for(int j=0;j<a.length;j++)
{
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]<a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
------解决方案--------------------
import java.util.*;
public class ArrayPaiXu
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("请输入5个整型数字:");
int[] a=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("请输入第"+(i+1)+"位学员的成绩:");
a[i]=input.nextInt();
}
Arrays.sort(a);
for(int i:a)
System.out.print(i);
}
}
------解决方案--------------------
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("请输入5个整型数字:");
int[] a=new int[5];
for(int i=0;i<5;i++)
{
System.out.println("请输入第"+(i+1)+"位学员的成绩:");
a[i]=input.nextInt();
//System.out.println(a[i]);
}
int temp;
for(int j=0;j<a.length/2;j++)
{
temp=a[j];
a[j]=a[a.length-j-1];
a[a.length-j-1]=temp;
}
for(int j=0;j<a.length;j++){
System.out.println(a[j]);
}
}
}