gpt4 book ai didi

Java选择排序,我的代码有什么问题?

转载 作者:行者123 更新时间:2023-11-29 04:51:46 25 4
gpt4 key购买 nike

<分区>

import java.util.Random;

public class Tester {
public static void main(String[] args) {
selectionSort(args);
}

private static void printArray(int[] anArray) {
for (int i = 0; i < anArray.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(anArray[i]);
}
}


public static void selectionSort(String[] args) {

int i, x = 0;

int l = 10;
Random r = new Random(271);
int array[] = new int[l];
for (i = 0; i < l; i++) {
array[i] = r.nextInt();
}

while (i < l) {

for (int j = 1; j <= i; j++) {
if (array[j] < array[x])
x = j;
}

i++;
}
printArray(array);
}

}

一切都很好,只是无法正确打印。当我打印时,我得到“-1061221096、-349834974、-1279215928、1452441141、-367008517、638966200、-464597014、1551135748、-446923224、542496703”,这是不正确。我相信每个数字都应该在 271 以下。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com