首页 > 解决方案 > 我无法获得列表的总页数

问题描述

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

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
}

标签: java

解决方案


你可以这样写calcTotalPages

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

或者使用StreamAPI 如下:

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

推荐阅读