无法使用冒泡排序来比较对象属性(Can't get bubble sort to work w/ comparing object properties)
对于一系列书籍,分配是从用户获取对象的属性,然后根据他们对作者,标题或页面计数的选择进行排序。 我的冒泡排序适用于页数,但不是当用户选择作者或标题时,所以我猜我错过了参考存储与值存储的东西,也许? 谢谢!
import javax.swing.*; import java.util.*; public class LibraryBookSort { public static void main(String[] args) { LibraryBook[] books = new LibraryBook[5]; LibraryBook tempBook = new LibraryBook(); String enteredAuthor = "", enteredTitle = ""; String enteredPageCount; String sortBy; String displayString = ""; int parsedSortBy; int parsedPageCount; int booksLength = books.length; // populate the array for (int x = 0; x < booksLength; ++x) books[x] = new LibraryBook(); // get property values from user for (int x = 0; x < booksLength; ++x) { enteredTitle = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's title:"); books[x].setTitle(enteredTitle); enteredAuthor = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's author:"); books[x].setAuthor(enteredAuthor); enteredPageCount = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's page count:"); parsedPageCount = Integer.parseInt(enteredPageCount); books[x].setPageCount(parsedPageCount); } // sort by property values sortBy = JOptionPane.showInputDialog("Choose option to sort by: (1) title, (2) author, or (3) page count"); parsedSortBy = Integer.parseInt(sortBy); while (parsedSortBy < 1 || parsedSortBy > 3) { sortBy = JOptionPane.showInputDialog("Invalid selection, please choose option to sort by: (1) title, (2) author, or (3) page count"); parsedSortBy = Integer.parseInt(sortBy); } if (parsedSortBy == 1) { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getTitle().compareTo(books[b+1].getTitle()) > 1) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } else if (parsedSortBy == 2) { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getAuthor().compareTo(books[b+1].getAuthor()) > 1) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } else { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getPageCount() > books[b+1].getPageCount()) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } for (int i = 0; i < booksLength; ++i) { displayString += (books[i].getTitle() + ", by " + books[i].getAuthor() + ". " + books[i].getPageCount() + " pages.\n"); } JOptionPane.showMessageDialog(null, "Books sorted by your choice:\n\n" + displayString); }}
For an array of books, the assignment is to get attributes of the objects from the user, then sort based on their choice of author, title, or page count. My bubble sort is working for the page count but not when the user chooses author or title, so I'm guessing I'm missing something with reference storage vs. value storage, maybe? Thanks!
import javax.swing.*; import java.util.*; public class LibraryBookSort { public static void main(String[] args) { LibraryBook[] books = new LibraryBook[5]; LibraryBook tempBook = new LibraryBook(); String enteredAuthor = "", enteredTitle = ""; String enteredPageCount; String sortBy; String displayString = ""; int parsedSortBy; int parsedPageCount; int booksLength = books.length; // populate the array for (int x = 0; x < booksLength; ++x) books[x] = new LibraryBook(); // get property values from user for (int x = 0; x < booksLength; ++x) { enteredTitle = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's title:"); books[x].setTitle(enteredTitle); enteredAuthor = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's author:"); books[x].setAuthor(enteredAuthor); enteredPageCount = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's page count:"); parsedPageCount = Integer.parseInt(enteredPageCount); books[x].setPageCount(parsedPageCount); } // sort by property values sortBy = JOptionPane.showInputDialog("Choose option to sort by: (1) title, (2) author, or (3) page count"); parsedSortBy = Integer.parseInt(sortBy); while (parsedSortBy < 1 || parsedSortBy > 3) { sortBy = JOptionPane.showInputDialog("Invalid selection, please choose option to sort by: (1) title, (2) author, or (3) page count"); parsedSortBy = Integer.parseInt(sortBy); } if (parsedSortBy == 1) { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getTitle().compareTo(books[b+1].getTitle()) > 1) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } else if (parsedSortBy == 2) { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getAuthor().compareTo(books[b+1].getAuthor()) > 1) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } else { for (int a = 0; a < booksLength - 1; ++a) { for (int b = 0; b < booksLength - 1; ++b) { if (books[b].getPageCount() > books[b+1].getPageCount()) { tempBook = books[b]; books[b] = books[b+1]; books[b+1] = tempBook; } } } } for (int i = 0; i < booksLength; ++i) { displayString += (books[i].getTitle() + ", by " + books[i].getAuthor() + ". " + books[i].getPageCount() + " pages.\n"); } JOptionPane.showMessageDialog(null, "Books sorted by your choice:\n\n" + displayString); }}
最满意答案
比较应该是
.... > 0不
.... > 1compareTo的契约是在相等的情况下返回零,而对于“较大”则为0,对于“较小”则为<0。 实际上,值为-1,0和+1。
The comparison should be
.... > 0Not
.... > 1The contract of compareTo is to return zero on equal and something >0 for "larger" and <0 for "smaller". In practice the values are -1, 0 and +1.
更多推荐
发布评论