java中int a=scan.nextInt();int b=scan.nextInt();int c=scan.nextInt();如何将其生成数组,并降升序
答案:1 悬赏:60 手机版
解决时间 2021-03-25 02:58
- 提问者网友:夢醒日落
- 2021-03-24 11:46
java中int a=scan.nextInt();int b=scan.nextInt();int c=scan.nextInt();如何将其生成数组,并降升序
最佳答案
- 五星知识达人网友:长青诗
- 2021-03-24 12:25
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入
int len = 3;
int[] nums = new int[len];
for (int i = 0; i < len; i++) {
nums[i] = scanner.nextInt();
}
// 升序排列(nums[i] > nums[j] 改为 nums[i] < nums[j] 则变为降序排列)
int temp;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (nums[i] > nums[j]) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
// 输出
for (int i = 0; i < len; i++) {
System.out.print(nums[i] + " ");
}
}
}
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入
int len = 3;
int[] nums = new int[len];
for (int i = 0; i < len; i++) {
nums[i] = scanner.nextInt();
}
// 升序排列(nums[i] > nums[j] 改为 nums[i] < nums[j] 则变为降序排列)
int temp;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (nums[i] > nums[j]) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
// 输出
for (int i = 0; i < len; i++) {
System.out.print(nums[i] + " ");
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯