已解决 帖子末尾的固定代码以及进度
如果我的问题不清楚或有任何遗漏,我谨向您致歉。我是编程和Java的新手,所以我可能尚不知道如何恰当地表达我的问题。我将设置您应该知道的内容,以帮助我解决我的问题,然后再提出。如果可以使事情变得更清楚,我将继续编辑这篇文章。
我有(7)个学生对象的数组arrStudents :(名字,姓氏,年级)(字符串,字符串,整数)
getGrade()返回成绩
我想创建一个对数组进行气泡排序并打印的方法,例如,这是排序之前的输出:
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
这是排序的输出
Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95
我有一些代码可以使用,但是它并没有帮助我。这是气泡排序给我的示例:
import java.util.Scanner;
class array {
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int [] a = new int [100];
int i, n = 0, min, max;
float total = 0;
System.out.println ("Enter integers separated by blanks (<Enter> <Ctrl-Z> to end):");
while (scan.hasNext()) {
a[n] = scan.nextInt();
n = n + 1;
}
min = a[0];
max = a[0];
for (i = 0; i < n; i++) {
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
total = total + a[i];
}
System.out.print ("You entered " + n + " numbers: ");
System.out.print ("Min = " + min + ", Max = " + max + ", Average = " + total/n);
int t, swap = 0;
do {
swap = 0;
for (i=0; i<n-1; i++)
if (a[i]>a[i+1]) {
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
swap++;
}
} while (swap>0);
System.out.print ("\nSorted: ");
for (i=0; i<n; i++) System.out.print (a[i] + " ");
System.out.println ();
}
}
我无法使用>运算符使用对象数组,因此II尝试使用arrStudents [i] .getGrade(),但是我不确定那是否正确,我无法获得正确的信息输出。
这是我一直在玩的代码:
public static void bubbleSort(Student [] arrStudents) {
int t, swap = 0;
do {
swap = 0;
for (i=0; i<arrStudents.length-1; i++){
int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
if (grade>grade+1) {
t=grade;
grade=gradePlus;
gradePlus=t;
swap++;
}
}
} while (swap>0);
}
编辑:修改后的代码(仍然需要修复)
public static void bubbleSort(Student [] arrStudents) {
int swap = 0;
do {
swap = 0;
for (i=0; i<arrStudents.length-1; i++){
int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
if (grade>gradePlus) {
Student tmp = arrStudents[i];
arrStudents[i] = arrStudents[i+1];
arrStudents[i+1]=tmp;
swap++;
System.out.println(tmp);
}
}
} while (swap>0);
}
输出修改后的代码(降序可以):
Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
Barack Obama 95
John Smith 90
John Smith 90
John Smith 90
John Smith 90
John Smith 90
Al Clark 80
Al Clark 80
Al Clark 80
Al Clark 80
Ann Miller 75
Ann Miller 75
固定代码(具有升序输出) -我只是对上次尝试的打印内容感到愚蠢
public static void bubbleSort(Student [] arrStudents) {
int swap = 0;
do {
swap = 0;
for (i=0; i<arrStudents.length-1; i++){
Student tmp = arrStudents[i];
int grade = arrStudents[i].getGrade();
int gradePlus = arrStudents[i+1].getGrade();
if (grade>gradePlus) {
arrStudents[i] = arrStudents[i+1];
arrStudents[i+1]=tmp;
swap++;
}
}
} while (swap>0);
System.out.print ("\nSorted: ");
for (i=0; i<arrStudents.length; i++)
System.out.print ("\n" + arrStudents[i]);
}
输出
Sorted:
Sue Taylor 55
George Bush 58
John Miller 65
Ann Miller 75
Al Clark 80
John Smith 90
Barack Obama 95
在这个问题上困扰了一段时间,所以任何帮助将不胜感激!
tl; dr帮我修复上面最接近的冒泡排序代码
编辑:还知道可能有更好的排序方式,但是对于此程序,我需要使用冒泡排序。
这种实际的排序发生在swap
零件中。元素(如果要排序的数组)必须移动才能使排序执行任何操作。
您必须交换arrStudents[i]
和arrStudents[i+1]
,因为它是arrStudents
您要排序的:
Student tmp = arrStudents[i];
arrStudents[i] = arrStudents[i+1];
arrStudents[i + 1] = tmp;
然后(也由@maczikasz指出),您测试条件是错误的。使用:
if (grade > gradePlus) {
// Do the swap as above, increment the swap counter
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句