任务描述:
输入三个整数 x,y,z(0<x<1000,0<y<1000,0<z<1000),请把这三个数由小到大输出。
任务要求:
代码示例:
package march0317_0331;
import java.util.Scanner;
public class m240320 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = new int[3];
System.out.println("请输入3个整数,用空格隔开(要求输入0-1000):");
for (int i = 0; i < 3; i++) {
int num = scanner.nextInt();
if (num < 0 || num > 1000) {
System.out.println("输入错误,程序退出~");
return;
}
nums[i] = num;
}
//按照升序排序
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
System.out.println("从大到小排序为:" + nums[0] + " " + nums[1] + " " + nums[2]);
}
}