Java将输入的数都存到数组里面,并排序
时间: 2024-04-29 07:25:21 浏览: 9
好的,这是一个很好的问题。以下是示例代码:
```
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要排序的数字个数:");
int n = scanner.nextInt();
int[] nums = new int[n];
System.out.println("请输入" + n + "个数字:");
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums);
System.out.println("排序后的结果为:");
for (int i = 0; i < n; i++) {
System.out.print(nums[i] + " ");
}
}
}
```
希望这能够帮到你!
相关问题
Java将输入的数全部存到数组里面,代码
可以是这样的:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.print(nums[i] + " ");
}
}
}
```
这个代码可以将输入的n个数全部存到一个数组里面,并输出这个数组。请问还有其他问题吗?
java键盘录入数存到两组数组,输入数组一,输出数组二
以下是Java代码实现:
```java
import java.util.Scanner;
public class InputOutputArrays {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入数组的长度:");
int length = scanner.nextInt();
int[] inputArray = new int[length];
int[] outputArray = new int[length];
System.out.println("请逐个输入数组元素:");
for (int i = 0; i < length; i++) {
inputArray[i] = scanner.nextInt();
outputArray[i] = inputArray[i];
}
System.out.println("输入数组为:");
for (int i = 0; i < length; i++) {
System.out.print(inputArray[i] + " ");
}
System.out.println("\n输出数组为:");
for (int i = 0; i < length; i++) {
System.out.print(outputArray[i] + " ");
}
}
}
```
运行结果示例:
```
请输入数组的长度:5
请逐个输入数组元素:
1
2
3
4
5
输入数组为:
1 2 3 4 5
输出数组为:
1 2 3 4 5
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)