当我尝试调用我的冒泡排序类时出现了一些错误。但我找不到它。这是我的冒泡排序课程,应该没问题:
public static void Sort2(double[] yourNumbers) {
double swap;
for(int i = 0; i < yourNumbers.length-1; i++){
for(int d = 0; d < yourNumbers.length-1; d++){
if(yourNumbers[d] > yourNumbers[d+1]){
//swap feature
swap = yourNumbers[d];
yourNumbers[d] = yourNumbers[d+1];
yourNumbers[d+1] = swap;
}
}
}
}
这是我的主要方法(在这里,当我调用该Sort2
方法时出现错误:
sort.java 类型中的方法 Sort2(double[]) 不适用于参数 (int)
System.out.println("Size");
int yourNumbers = scan.nextInt();
double[] array = new double[yourNumbers];
System.out.println("Numbers");
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
array[i] = scan.nextDouble();
}
Sort2(array);
我知道这里被问了很多次,但我觉得我做错了。请问你能帮我吗?PS:我不允许使用ArrayList
.
编辑:这是我现在使用的代码。仍然无法工作:编辑 2:在这个http://www.leepoint.net/notes-java/data/arrays/70sorting.html的帮助下,我明白了。一条线解决一切
import java.util.Scanner;
public class Bublesort{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Size");
int yourNumbers = scan.nextInt();
double[] array = new double[yourNumbers];
System.out.println("Numbers");
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
array[i] = scan.nextDouble();
}
Sort2(array);
**System.out.println(Arrays.toString(array));**
}
public static void Sort2(double[] yourNumbers) {
double swap;
for(int i = 0; i < yourNumbers.length - 1; i++){
for(int d = i + 1; d < yourNumbers.length; d++){
if(yourNumbers[i] > yourNumbers[d]){
//swap feature
swap = yourNumbers[i];
yourNumbers[i] = yourNumbers[d];
yourNumbers[d] = swap;
}
}
}
}}