誰かがこのバブルソートを機能させるのを手伝ってくれませんか? 私は Java の初心者であり、ソートや配列をこれまであまり使用したことがありません。また、メニューから if ステートメントを使用して初期化する必要があります。
バブルソート法:
private void sort(int grade, Student[] students) {
int temp = 0; // temporary holding area for swap
int i, j;
if (students.length < 2) {return;}
// Loop through length of the array
for (Student student : students) {
for (i = 0; i < students.length; i++ ) {
// Check to see if there is anything smaller and replace
for ( j = i+1; j < students.length - 1; j++) {
if (student[i] > student[j])
{
temp = student[i];
student[i] = student[j];
student[j] = temp;
これまでのメニュー: (バブル ソートを機能させるには、オプション 7 を追加する必要がありますか??)
public static void UserSelection(Scanner input, int numStudents,
Student[] students) {
// Repeatedly process menu selections by the user.
int choice;
do {
System.out.println();
System.out.println("*** Student Exam Results Menu ***");
System.out.println("(1) Display the results");
System.out.println("(2) Display the average result");
System.out.println("(3) Display the highest grade");
System.out.println("(4) Display the lowest grade");
System.out.println("(5) Search for specific result");
System.out.println("(6) Search for student grade by name");
System.out.println("(7) Sort the results in ascending order");
System.out.println("(8) quit");
choice = input.nextInt();
if (choice == 1)
{
System.out.println(Arrays.toString(students));
} else if (choice == 2)
{
double average = getAverage(students);
System.out.println("average grade = " + average);
} else if (choice == 3)
{
int high = getHighest(students);
System.out.println("high grade = " + high);
} else if (choice == 4)
{
int low = getLowest(students);
System.out.println("low grade = " + low);
} else if (choice == 5)
{
System.out.print("Result to look for: ");
int grade = input.nextInt();
if (result(grade, students)) {
System.out.println(grade +
" is in the collection of grades.");
} else
{
System.out.println(grade +
" is not in the collection of grades.");
}
} else if (choice == 6)
{
System.out.print("Student to search for: ");
String name = input.nextLine();
if (search(name, students) ==null)
{
System.out.println(name +
" is in the list of Students.");
} else
{
System.out.println(name +
" is not in the list of Students");
}
}