我仍然无法做到这一点。冒泡排序中的代码不正确。我怎样才能做到这一点?我应该更改或添加什么以获得正确的结果?提前致谢。:)
import java.util.Random;
import java.util.Scanner;
public class HomeWork {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int choice;
int e;
Random t = new Random();
for (e = 1; e <= 5; e++) {
System.out.println(t.nextInt(1000));
}
System.out.println(" \n1: BUBBLE SORT ");
System.out.println(" 2: SELECTION SORT ");
System.out.println(" 3: QUICK SORT ");
System.out.println(" Choose a number from 1-3 ");
choice= s.nextInt();
if(choice == 1) {
System.out.print("You chose BUBBLE sort!");
int temp, q, w;
for(int i=0;i<w-1;i++) { //I think there is something wrong here in my bubble sort code.
// What should I add or change to make this correct?
for(int j=0;j<w-1-i;j++) {
if(q[j]>q[j+1]) {
temp = q[j];
q[j] = q[j+1];
q[j+1] = temp;
System.out.println(q[i]+""); // What should I change here to print the correct results?
} else if(choice == 2) {
System.out.print("You chose SELECTION sort!");
} else if(choice == 3) {
System.out.println("You chose QUICK sort!");
} else {
System.out.println("Not in the choices!");
}
}
}
}
}
}
我仍然只是一个初学者。请帮忙。提前致谢 :)