求大神,这道题怎么做
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
2017-03-14
这是运行截图
唯一美中不足的就是那些红框标的数字,应该能凑合着吧
package student;
import java.util.Scanner;
//数组排序
public class Answer {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("请输入数组长度");
int a=s.nextInt();
int b[]=new int[a];
for (int i = 0; i < b.length; i++) {
System.out.println("请输入第"+(i+1)+"个元素");
b[i]=s.nextInt();
}
System.out.println("排序前:");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+"\t");
}
System.out.println();
int temp;
//正序排序
for (int i = 0; i < b.length-1; i++) {
for (int j = b.length-1; j >i; j--) {
if(b[j]<b[j-1]){
temp=b[j];
b[j]=b[j-1];
b[j-1]=temp;
}
}
}
System.out.println("排序后:");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+"\t");
}
//逆序排序
for (int i = 0; i < b.length-1; i++) {
for (int j = b.length-1; j >i; j--) {
if(b[j]>b[j-1]){
temp=b[j-1];
b[j-1]=b[j];
b[j]=temp;
}
}
}
System.out.println();
System.out.println("逆序排出:");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+"\t");
}
s.close();
}
}