java输入三个整数x,y,z,对齐进行排序,使得x<y<z
时间: 2023-05-28 15:02:34 浏览: 66
您可以使用Java的内置排序函数来按数字顺序对三个整数进行排序。对于较小的数据集,Bubble sort 或 selection sort 可能比较合适,但对于较大的数据集来说,Quick sort 或 Merge sort 更适合。
下面是一个使用Bubble Sort排序算法的Java代码示例:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入三个整数:");
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
int temp;
if (x > y) {
temp = x;
x = y;
y = temp;
}
if (x > z) {
temp = x;
x = z;
z = temp;
}
if (y > z) {
temp = y;
y = z;
z = temp;
}
System.out.println("排序后的结果为:" + x + " " + y + " " + z);
}
}
```
此程序通过比较三个整数并根据需要交换其位置,以确保它们按升序排列。最后,程序打印排序后的整数。