用java将三个数字从小到大排序 (15分) 从键盘输入三个整数,按照升序排列后输出。
时间: 2023-09-09 13:01:51 浏览: 191
要使用Java将三个数字从小到大排序,首先需要从键盘输入这三个整数。可以使用Java的Scanner类来实现。接下来,可以使用条件判断来比较这三个数字的大小,并按照升序排列后输出。
下面是一个简单的示例代码:
```java
import java.util.Scanner;
public class SortNumbers {
public static void main(String[] args) {
// 从键盘输入三个整数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三个整数:");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
// 对三个数字进行升序排序
int temp;
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
// 输出排序后的结果
System.out.println("升序排列后的结果为:" + num1 + " " + num2 + " " + num3);
}
}
```
通过以上代码,我们可以将输入的三个整数按照升序排列后输出。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)