我随便输入3个整数,让这三个数按大小顺序排列出来,Java代码
时间: 2024-05-14 15:14:26 浏览: 16
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
System.out.println(a + " " + b + " " + c);
}
}
```
输入示例:
```
5 2 8
```
输出示例:
```
2 5 8
```
相关问题
java输入三个整数x,y,z,对齐进行排序,使得x<y<z
您可以使用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);
}
}
```
此程序通过比较三个整数并根据需要交换其位置,以确保它们按升序排列。最后,程序打印排序后的整数。
(1)构造三个线程对象,分别输出80以内的2、3、5的倍数。让程序执行3次,对3次运行结果进行分析。java
下面是一个可能的实现:
```java
public class MultiplesThread extends Thread {
private int n;
public MultiplesThread(int n) {
this.n = n;
}
public void run() {
for (int i = 1; i <= 80; i++) {
if (i % n == 0) {
System.out.println(getName() + ": " + i);
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
MultiplesThread t2 = new MultiplesThread(2);
MultiplesThread t3 = new MultiplesThread(3);
MultiplesThread t5 = new MultiplesThread(5);
t2.start();
t3.start();
t5.start();
try {
t2.join();
t3.join();
t5.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
在这个实现中,我们定义了一个 `MultiplesThread` 类,它继承了 `Thread` 类,并且包含一个整数 `n`,表示要输出 `n` 的倍数。`run` 方法中,我们遍历从 1 到 80 的所有数字,如果当前数字是 `n` 的倍数,就输出它。在 `main` 方法中,我们创建了三个 `MultiplesThread` 对象,分别输出 2、3、5 的倍数,并且启动它们。我们使用 `join` 方法等待三个线程全部结束,然后重复执行三次。
如果我们运行这个程序,可能会得到如下的输出:
```
Thread-0: 2
Thread-1: 3
Thread-2: 5
Thread-2: 10
Thread-0: 4
Thread-1: 6
Thread-2: 15
Thread-0: 6
Thread-1: 9
Thread-2: 20
Thread-2: 25
Thread-0: 8
Thread-1: 12
Thread-2: 30
...
```
可以看到,输出的数字并不是按照顺序排列的,因为三个线程是并发执行的,它们的执行顺序是不确定的。另外,每次运行的结果也可能会有所不同,因为线程的调度是由操作系统负责的,而操作系统的行为是不确定的。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)