gpt4 book ai didi

java - 我无法获取列表的总页数

转载 作者:行者123 更新时间:2023-12-01 16:36:04 27 4
gpt4 key购买 nike

这是我到目前为止的代码,如果有人可以帮助我,我还需要计算总页数,但我无法管理它......

package assignment2;
import java.util.*;
import static java.lang.System.*;
public class Book {
private String title;
//Private class fields
private int numOfPages;
public String getTitle() {
//Getter
return title;
}

public void setTitle(String title) {
//Setter
this.title = title;
}

public int getNumOfPages() {
return numOfPages;
}

public void setNumOfPages(int numOfPages) {
this.numOfPages = numOfPages;
}

public Book(String title, int pages) {
//A parameterized constructor with two parameters
this.setTitle(title);
this.setNumOfPages(numOfPages);
}

public static class Main {
public static void main(String[] args) {
List<Book> books = new LinkedList<>();
books.add(new Book("The Catcher in the Rye", 190));
//List of Book instances
books.add(new Book("Song of Myself", 259));
books.add(new Book("A Man Called Ove", 295));
books.add(new Book("Lolita", 360));
books.add(new Book("The Diary of a Young Girl", 352));
books.add(new Book("The Tattooist of Auschwitz", 288));
books.add(new Book("The Notebook", 214));
List<Book> temporaryBook = new ArrayList<>(books);
//Sublist that stops main method
BookApp bkapp = new BookApp();
int totalPages = bkapp.calcTotalPages(temporaryBook, 0);
out.printf("Total number of books: %d\n", books.size());
out.printf("Total pages: %d\n", totalPages);
}
}

//End of Main class
public static class BookApp {
public int calcTotalPages(List<Book> bookList, int accum) {
if (bookList.size() == 0)
return accum;
return calcTotalPages(bookList, accum + bookList.remove(0).getNumOfPages());
}
}

//End of class BookApp
}

最佳答案

您可以按如下方式编写calcTotalPages:

public static int calcTotalPages(List<Book> bookList) {
int sum = 0;
for (Book book : bookList) {
sum += book.getNumOfPages();
}
return sum;
}

或者使用Stream API,如下所示:

public static int calcTotalPages(List<Book> bookList) {
return bookList.stream().mapToInt(Book::getNumOfPages).sum();
}

演示:

import java.util.LinkedList;
import java.util.List;

class Book {
private String title;
private int numOfPages;

public Book(String title, int numOfPages) {
this.title = title;
this.numOfPages = numOfPages;
}

public String getTitle() {
return title;
}

public int getNumOfPages() {
return numOfPages;
}
}

public class BookApp {
public static class Main {
public static void main(String[] args) {
List<Book> books = new LinkedList<>();
books.add(new Book("The Catcher in the Rye", 190));
books.add(new Book("Song of Myself", 259));
books.add(new Book("A Man Called Ove", 295));
books.add(new Book("Lolita", 360));
books.add(new Book("The Diary of a Young Girl", 352));
books.add(new Book("The Tattooist of Auschwitz", 288));
books.add(new Book("The Notebook", 214));

int totalPages = calcTotalPages(books);
System.out.printf("Total number of books: %d\n", books.size());
System.out.printf("Total pages: %d\n", totalPages);
}
}

public static int calcTotalPages(List<Book> bookList) {
return bookList.size() == 0 ? 0 : bookList.stream().mapToInt(Book::getNumOfPages).sum();
}
}

输出:

Total number of books: 7
Total pages: 1958

关于java - 我无法获取列表的总页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61944110/

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