我的返回值总是等于0

时间:2016-11-08 05:32:01

标签: java arrays methods indexing return-value

我编写了多个方法,这些方法使用数组来确定某个值,但它们都是0,我不知道为什么。目标是在库中创建教科书列表,然后返回最重的书籍,重量,索引,平均页数和总页数。

public class Project3Driver {

// Data Fields

public static final int SUBJECTS = 5;
public static final int TEXTBOOKS = 10000;
String[] SUBJECT_LIST = {"Biology", "Calculus", "Linear Algebra", "Geology", "C++"};
Textbook[] library = new Textbook[TEXTBOOKS];
Random rand = new Random();

// Constructor that uses min-max system to randomize page count from 500-1500 and randomizes the subject

public Project3Driver() {
    for (int i = 0; i < library.length; i++) {
        library[i] = new Textbook(SUBJECT_LIST[rand.nextInt(SUBJECTS)], 500 + (int) (Math.random() * ((1500 - 500) + 1)));
    }
}

// Methods 

// Finds the heaviest book and returns the weight
public double findHeaviest() {
    double heaviestBook = library[0].getWeight();
    for (int i = 1; i < library.length; i++) {
        if (library[i].getWeight() > heaviestBook) {
            heaviestBook = library[i].getWeight();
        }
    }
    return heaviestBook;
}

// Finds the heaviest book and returns the index
public int getHeaviest() {
    double heaviestBook = library[0].getWeight();
    int index = 0;
    for (int i = 1; i < library.length; i++) {
        if (library[i].getWeight() > heaviestBook) 
        {
            heaviestBook = library[i].getWeight();
            index = i;
        }
    }
    return index;
}

// Returns the average page count of the library
public int computeAverageSize() {
    int pageCount = 0;
    for (int i = 0; i < library.length; i++) 
    {
        pageCount = pageCount + library[i].getPageCount();
    }
    pageCount = pageCount / library.length;
    return pageCount;
}

// Returns the total page count of the library
public int computeTotalPages() 
{
    int pageCount = 0;
    for (int i = 0; i < library.length; i++) 
    {
        pageCount = pageCount + library[i].getPageCount();
    }
    return pageCount;
}

// Tests each method and prints it (called in the main function)
public void getLibraryStats() 
{
    System.out.println("Heaviest book is " + library[getHeaviest()].getSubject());
    System.out.println("The heaviest book is " + findHeaviest() + " kg");
    System.out.println("The heaviest book is at the index " + getHeaviest());
    System.out.println("The average book size is " + computeAverageSize());
    System.out.println("There are " + computeTotalPages() + " pages total");
}

}

getPageCount方法只返回页数,getWeight返回权重(乘以pageCount * 0.0025)

任何帮助将不胜感激!我觉得这可能是我的阵列的一个问题,但我多次检查错误

编辑:

public class Textbook {

public static final double PAGE_WEIGHT = 0.0025;
public static final int PAGE_KNOWLEDGE = 5;
private String subject;
private int pageCount;
private int unreadPages;

// Start of Constructors
    // Default constructor
public Textbook() {
    subject = "Object-Oriented Programming";
    pageCount = 800;
    unreadPages = pageCount;
}

// Constructor with subject only
public Textbook(String textSubject) {
    subject = textSubject;
}
// End subject constructor

// Constructor with subject and page count
public Textbook(String bookSubject, int bookPages) {
    subject = bookSubject;
    unreadPages = pageCount;
} // End subject and page count constructor
// End of Constructors

// Start of Accessor Methods
// Method to return text subject
public String getSubject() {
    return subject;
}

// Method to return page count
public int getPageCount() {
    return pageCount;
}

// Method to return unread pages
public int getUnreadPageCount() {
    return unreadPages;
}

// Method to get weight
public double getWeight() {
    return pageCount * PAGE_WEIGHT;
}

// Method to read the pages
public int readPages(int numPages) {
    if (numPages > pageCount) {
        numPages = pageCount;
    }
    int knowledgeGained = 0;
    if (unreadPages == 0) {
        knowledgeGained = numPages * 5 / 2;
    } else if (unreadPages <= numPages) {
        knowledgeGained = unreadPages * 5 + (numPages - unreadPages) * 5 / 2;
        unreadPages = 0;
    } else {
        knowledgeGained = numPages * 5;
        unreadPages -= numPages;
    }
    return knowledgeGained;
}
}

public class CSC211Project3 
{
    public static void main(String[] args) 
    {
        Project3Driver librarytester = new Project3Driver();
        librarytester.getLibraryStats();
    }   
}

2 个答案:

答案 0 :(得分:2)

您的Textbook构造函数未初始化pageCount,因此默认为0

要修复它,请初始化字段:

public Textbook(String bookSubject, int bookPages) {
    subject = bookSubject;
    pageCount = bookPages;
    unreadPages = bookPages;
}

答案 1 :(得分:0)

尝试在for循环中使用system out语句来使用断点进行调试或调试。 使用系统调试,

System.out.println("getting formed book weight "+library[i].getWeight())

Textbook类构造函数

的逻辑可能有问题