【发布时间】:2021-07-03 21:11:56
【问题描述】:
所以我目前遇到了这个问题,当我运行我的程序时,它会重复第二个显示行。 (“未排序的行”)
我在运行程序时也遇到了对齐问题
我需要这个程序做什么:
-
生成 10 个介于 1 和 100 之间的随机整数,并将每个随机数放入一维数组的不同元素中,从生成的第一个数字开始。
-
找出 10 个数字中最大的一个并显示它的值。
-
按照最初插入数字的顺序显示数组的内容。这称为未排序列表。
-
现在使用冒泡排序将数组从最小整数排序到最大整数。冒泡排序必须有自己的方法;不能在 main 方法中。
这是我目前编写的程序。我只是在运行时需要帮助进行调整。
import java.util.Arrays;
public class Chpt7_Project2 {
//Ashley Snyder
public static void main(String[] args) {
//create an array of 10 integers
int[] list = new int[10];
//initialize array of 10 random integers between 0 and 100
for (int i = 0; i < list.length; i++) {
list[i] = (int) (Math.random() * 100 + 1);
}
//Find the maximum of the list of random numbers generated
int maximum = -1;
int minimum = 999;
for (int i = 0; i < list.length; i++) {
if (maximum < list[i])
maximum = list[i];
if (minimum > list[i])
minimum = list[i];
}
//Display the maximum from the randTen array
System.out.println("The largest value is: " + maximum);
//Display the unsorted list of numbers from the randTen array
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + "The unsorted list is: ");
}
//Display the sorted array numbers
bubbleSort(list);
System.out.println("The sorted list is: " + Arrays.toString(list) + " ");
}
public static void bubbleSort(int[] list) {
//Sort randomly generated integers in randArray from lowest to highest
int temp;
for (int i = list.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
【问题讨论】:
-
我会考虑将您的标签从 javascript 更改为 java。
-
程序按预期产生输出。您是否正在寻找纠正输出对齐方式的方法,例如在单独的行上打印?
-
如果我能注意到:“找出前 10 个最高的数字”已经与“对您的数字进行排序,然后打印最后 10 个”是相同的,而且 Collections.sort 很乐意为您服务。此外,虽然不太可能对家庭作业有用,但请注意 copyOfRange 存在,并且相当有用。最后一点:记得查看您的帖子,如果您发现任何不好的缩进,编辑您的帖子以清理它。 Good posts得到好的答案
-
我在第二行重复和对齐方面遇到问题
标签: java arrays sorting bubble-sort