无法使用冒泡排序来比较对象属性(Can't get bubble sort to work w/ comparing object properties)

编程入门 行业动态 更新时间:2024-10-27 18:25:48
无法使用冒泡排序来比较对象属性(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

.... > 1

compareTo的契约是在相等的情况下返回零,而对于“较大”则为0,对于“较小”则为<0。 实际上,值为-1,0和+1。

The comparison should be

.... > 0

Not

.... > 1

The 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.

更多推荐

本文发布于:2023-07-04 16:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   bubble   sort   properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!