temp.compareTo 错误。不能取消引用 Int
temp.compareTo error. Int cannot be be dereferenced
我正在尝试创建一个按升序对五个整数进行排序的程序。我以前从未 运行 陷入取消引用的错误,所以我很好奇我做错了什么。
Scanner input = new Scanner(System.in);
int[] a = new int[5];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter integer # "+ 1 + i);
int temp = input.nextInt();
a[i] = temp;
}
System.out.println("Sorted from lowest to highest");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int temp = a[i];
int tempB = a[j];
if (temp.compareTo(tempB) < 0) {
a[i] = tempB;
a[j] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(a[i]);
}
}
}
我在这一行收到错误。
if (temp.compareTo(tempB) < 0)
谢谢!
temp
是int类型,没有方法。你应该写
if(temp < tempB)
您不能在 int
上调用 compareTo
方法(原始类型)。
使用:
if(temp < tempB)
我正在尝试创建一个按升序对五个整数进行排序的程序。我以前从未 运行 陷入取消引用的错误,所以我很好奇我做错了什么。
Scanner input = new Scanner(System.in);
int[] a = new int[5];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter integer # "+ 1 + i);
int temp = input.nextInt();
a[i] = temp;
}
System.out.println("Sorted from lowest to highest");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int temp = a[i];
int tempB = a[j];
if (temp.compareTo(tempB) < 0) {
a[i] = tempB;
a[j] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(a[i]);
}
}
}
我在这一行收到错误。
if (temp.compareTo(tempB) < 0)
谢谢!
temp
是int类型,没有方法。你应该写
if(temp < tempB)
您不能在 int
上调用 compareTo
方法(原始类型)。
使用:
if(temp < tempB)