从键盘任意输入5个数字,在函数sort(int a[],char style)中进行5个数字排序
答案:2 悬赏:30
解决时间 2021-04-08 06:15
- 提问者网友:猖狂醉薇
- 2021-04-07 06:38
要求:排序方式根据函数的style参数进行,如style为'a'按升序排,style为'd'按降序排
最佳答案
- 二级知识专家网友:瘾与深巷
- 2021-04-07 07:28
package Test;
import java.util.Scanner;
public class test {
public static void sort(int[] a, char style) {
if ('a' == style) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] < a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
} else if ('b' == style) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
for (int i : a) {
System.out.println(i);//输出排序之后的
}
}
public static void main(String[] args) {
int[] a = new int[5];
System.out.println("请输入5个数字");
for (int i = 0; i < a.length; i++) {
a[i]=new Scanner(System.in).nextInt();
}
System.out.println("输入排序类型:");
String c=new Scanner(System.in).next();
char[] cc=c.toCharArray();
char style=cc[0];
sort(a,style);
}
}
import java.util.Scanner;
public class test {
public static void sort(int[] a, char style) {
if ('a' == style) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] < a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
} else if ('b' == style) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
for (int i : a) {
System.out.println(i);//输出排序之后的
}
}
public static void main(String[] args) {
int[] a = new int[5];
System.out.println("请输入5个数字");
for (int i = 0; i < a.length; i++) {
a[i]=new Scanner(System.in).nextInt();
}
System.out.println("输入排序类型:");
String c=new Scanner(System.in).next();
char[] cc=c.toCharArray();
char style=cc[0];
sort(a,style);
}
}
全部回答
- 1楼网友:短发女王川岛琦
- 2021-04-07 08:26
期待看到有用的回答!
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯