【发布时间】:2017-04-25 15:46:18
【问题描述】:
尝试编写代码,以便通过冒泡排序对用户输入的数字列表进行排序。这是我到目前为止所拥有的:
import java.util.Scanner;
public class BubbleSort
{
public static void main(String[] args)
{
int n;
int[] list[];
System.out.println("Please enter number of the elements to be sorted");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for ( int pass = 1; pass < n; pass++)
for (int i = 0; i < n - pass ; i++)
{
if (list[i] > list[i + 1]){
int temp = list[n];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
}
我收到以下错误提示
The operator > is undefined for the argument type(s) int[], int[]" for the line: if (list[i] > list[i + 1])
type mismatch: cannot convert from int[] to int" for line: int temp = list[n];
Type mismatch: cannot convert from int to int[]" for line: list[i + 1] = temp;
非常感谢您的宝贵时间和帮助。
【问题讨论】:
-
您的气泡交换中至少存在一个错误。
int temp = list[n];应该是int temp = list[i];
标签: java arrays sorting bubble-sort