1 Star 0 Fork 0

韩超/韩超

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ShiYan.java 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
韩超 提交于 2024-05-10 16:25 +08:00 . lab4
package zu;
import java.util.Scanner;
public class ShiYan {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
System.out.print("请输入整数的个数:");
while (!scanner.hasNextInt()) {
System.out.println("输入的不是整数,请重新输入整数的个数:");
scanner.next();
}
n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("请输入" + n + "个整数:");
for (int i = 0; i < n; i++) {
while (!scanner.hasNextInt()) {
System.out.println("输入的不是整数,请重新输入第" + (i + 1) + "个整数:");
scanner.next();
}
arr[i] = scanner.nextInt();
}
scanner.close();
bubbleSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Han-chao_12345/han-chao.git
git@gitee.com:Han-chao_12345/han-chao.git
Han-chao_12345
han-chao
韩超
master

搜索帮助