gpt4 book ai didi

java - 我的返回值始终等于 0

转载 作者:行者123 更新时间:2023-12-02 03:14:45 24 4
gpt4 key购买 nike

我编写了多种使用数组来确定某个值的方法,但它们的结果都是 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();
}
}

最佳答案

您的 Textbook 构造函数不会初始化 pageCount,因此它将默认为 0

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

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

关于java - 我的返回值始终等于 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40479654/

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