提问人:stupidmoron 提问时间:10/6/2022 更新时间:10/6/2022 访问量:75
为什么我的数组在那里找不到?
Why is my array not found when it is so there?
问:
我的 jdk 编译器一直告诉我,我的数组(称为 nums)在它存在时找不到!什么给了?请原谅我的无知;我只是在学习。无论如何,我的代码在下面列出。
import java.util.Random;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
Random rand = new Random();
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++)
nums[i] = rand.nextInt(100);
System.out.println("Unsorted array: " + Arrays.toString(nums));
sort();
System.out.println("Sorted array: " + Arrays.toString(nums));
}
public static void sort() {
int temp;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - i - 1; j++) {
if (nums[j] > nums[j+1]) {
temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
}
}
答:
1赞
Unmitigated
10/6/2022
#1
nums
在定义它的方法之外不可见 ()。将其作为参数传递给。main
sort
更改 的签名以接受数组参数:sort
public static void sort(int[] nums)
传递到:nums
sort
main
sort(nums);
下一个:递归 - 导致堆栈溢出的原因
评论
main
sort
num
main