gpt4 book ai didi

Java 数组、实例化和排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:52 26 4
gpt4 key购买 nike

我正在完成 Java 类(class)的家庭作业问题,该类(class)与实例化、数组和数组数据的排序有关。这是问题:

Create a class named LibraryBook that contains fields to hold methods for setting and getting a LibraryBook's title, author, and page count. Save the file as LibraryBook.java.

Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields. Then prompt the user to enter which field the LibraryBooks should be sorted by - title, author, or page count. Perform the requested sort procedure and display the LibraryBook objects. Save the file as LibraryBookSort.java.

除了这本书之外,教授还添加了以下标准:

Declare an array of LibraryBook objects and sort them either by title, author or page count, as the user requests.

这是我为 LibraryBook.java 编写的代码:

public class LibraryBook
{
String bookTitle;
String bookAuthor;
int bookPageCount;

public LibraryBook(String title, String author, int count)
{
bookTitle = title;
bookAuthor = author;
bookPageCount = count;
}

public String getBookTitle()
{
return bookTitle;
}

public String getBookAuthor()
{
return bookAuthor;
}

public int getBookPageCount()
{
return bookPageCount;
}
}

这是我目前为 LibraryBookSort.java 编写的代码:

import java.util.Arrays;
import java.util.Scanner;

public class LibraryBookSort
{
public static void main(String[] args)
{

LibraryBook[] book = new LibraryBook[5];

book[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
book[1] = new LibraryBook("Team Of Rivals", "Dorris Kearns Goodwin", 994);
book[2] = new LibraryBook("1776", "Daivd McCullough", 400);
book[3] = new LibraryBook("No Ordinary Time", "Dorris Kearns Goodwin", 768);
book[4] = new LibraryBook("Steve Jobs", "Walter Isaacson", 656);

for (int x = 0; x < book.length; ++x)
book[x].getBookTitle();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookTitle());

for (int x = 0; x < book.length; ++x)
book[x].getBookAuthor();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookAuthor());

for (int x = 0; x < book.length; ++x)
book[x].getBookPageCount();
for (int x = 0; x < book.length; ++x)
System.out.println(book[x].getBookPageCount());
}
}

上面的代码似乎可以工作并显示所有数据,尽管格式不正确。我希望数据如下所示:

Java Programming       Joyce Farrel               881
Team Of Rivals Dorris Kearns Goodwin 994
1776 Daivd McCullough 400
No Ordinary Time Dorris Kearns Goodwin 768
Steve Jobs Walter Isaacson 656

除了像上面那样格式化输出之外,我还需要让每种数据类型(标题、作者、页面)都可以根据用户的选择进行排序。

在这一点上,我只是迷路了。到目前为止,这超出了我的技能水平。我希望有人能给我一些关于此时去哪里的指示/方向。

最佳答案

关于格式,您只需要一个 for 循环即可显示所有详细信息,如下所示:

for (int x = 0; x < book.length; ++x){
System.out.println(book[x].getBookTitle() + "\t" + book[x].getBookAuthor() + "\t" + book[x].getBookPageCount());
}

通过这种方式,您可以一次获得关于每本书的所有必要信息,每次输出一个句子,值由制表符 (\t) 分隔。

你现在做的一半循环都是无用的,比如这个:

for (int x = 0; x < book.length; ++x)
book[x].getBookTitle();

您没有在任何变量中获取标题,因此您只是在整个数组上执行一个循环。

对于排序选项,您可以查看 Array 的 sort 方法(如果您被允许使用它),或者尝试自己实现一个排序方法,这可能会对您的知识有所帮助数组和数组迭代。

关于Java 数组、实例化和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8381063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com