返回
阅读【小若大人】的帖子
我的第一个JAVA程序
版块:Aide
把任意五个数字按从大到小的顺序排列
我特么写了八十行
话说有没有简单点的方法呀,如果是一百个数那我得写多少行
import java.util.Scanner;public class Main
{ public static void main(String[] args)
{ Scanner s = new Scanner(System.in);
System.out.println("请输入第一个数字:"); int a = s.nextInt();
System.out.println("请输入第二个数字:"); int b = s.nextInt();
System.out.println("请输入第三个数字:"); int c = s.nextInt();
System.out.println("请输入第四个数字:"); int d = s.nextInt();
System.out.println("请输入第五个数字:"); int e = s.nextInt();
int x; if(b > a)
{ x = a;
a = b; b = x;
} if(c > a)
{ x = a;
a = c; c = x;
} if(d > a)
{ x = a;
a = d; d = x;
} if(e > a)
{ x = a;
a = e; e = x;
} if(c > b)
{ x = b;
b = c; c = x;
} if(d > b)
{ x = b;
b = d; d = x;
} if(e > b)
{ x = b;
b = e; e = x;
} if(d > c)
{ x = c;
c = d; d = x;
} if(e > c)
{ x = c;
c = e; e = x;
} if(e > d)
{ x = d;
d = e; e = x;
} System.out.println("这五个数字从大到小排列分别为:");
System.out.println(a+","+b+","+c+","+d+","+e); }
}
我特么写了八十行
话说有没有简单点的方法呀,如果是一百个数那我得写多少行
import java.util.Scanner;public class Main
{ public static void main(String[] args)
{ Scanner s = new Scanner(System.in);
System.out.println("请输入第一个数字:"); int a = s.nextInt();
System.out.println("请输入第二个数字:"); int b = s.nextInt();
System.out.println("请输入第三个数字:"); int c = s.nextInt();
System.out.println("请输入第四个数字:"); int d = s.nextInt();
System.out.println("请输入第五个数字:"); int e = s.nextInt();
int x; if(b > a)
{ x = a;
a = b; b = x;
} if(c > a)
{ x = a;
a = c; c = x;
} if(d > a)
{ x = a;
a = d; d = x;
} if(e > a)
{ x = a;
a = e; e = x;
} if(c > b)
{ x = b;
b = c; c = x;
} if(d > b)
{ x = b;
b = d; d = x;
} if(e > b)
{ x = b;
b = e; e = x;
} if(d > c)
{ x = c;
c = d; d = x;
} if(e > c)
{ x = c;
c = e; e = x;
} if(e > d)
{ x = d;
d = e; e = x;
} System.out.println("这五个数字从大到小排列分别为:");
System.out.println(a+","+b+","+c+","+d+","+e); }
}
(adsbygoogle = window.adsbygoogle || []).push({});
- 0
- 0收藏帖子
- 0关注帖子
- 0
本贴有需要购买的内容
附件列表
共 211 条评论
排序:时间 降序
请文明交流~
第1页,共计11页
- 1
- 2
- 3
- 4
- 5
- 6
- 11