gpt4 book ai didi

java - (初学者 Java 问题)为什么这段代码不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:11 27 4
gpt4 key购买 nike

(完整代码如下)我刚刚交了一个实验室上课,我们必须开设一门描述某本书的类(class)。我不知道如何做两件事。1. 如果有人为 'pages' 或 'suggestedRetailPrice' 输入小于零的值,该值必须设置为零。在此代码中,即使值为正值,该值也设置为 0。在:


if ( pages <= 0 )
{
pages = 0;
}

如果我将第二个“0”设置为不同的数字,请编写代码,比如:


if ( pages <= 0 )
{
pages = 1;
}

然后,无论您为“页面”输入什么,其值都将是 1。但如果您输入的值是负数,它不应该是 1 吗?我不明白我做错了什么。

我无法弄清楚的第二件事是在代码的底部,我们必须显示所有信息。我的老师希望我们将这本书是否平装显示为"is"或“否”,而不是“真”或“假”。我该怎么做呢?我试过像这样放置一个 if/else 语句:System.out.println("Paperback : "+ if (paperback = true) {Yes} if (paperback = false) {no}; )

没用,想不通。请参阅下面的完整代码。


public class Book {
// Instance variables
private String title;
private String author;
private int isbn;
private int pages;
private boolean paperback;
private int suggestedRetailPrice;

/**
* Default contructor
*/
public Book() {
title = "";
author = "";
isbn = 0;
pages = 0;
paperback = false;
suggestedRetailPrice = 0;
}

/**
* book information
*/
public Book(String whatIsTitle, String whoIsAuthor, int isbnCode,
int numberOfPages, boolean isItPaperback,
int theSuggestedRetailPrice) {
title = whatIsTitle;
author = whoIsAuthor;
isbn = isbnCode;
if (pages <= 0) {
pages = 0;
} else {
pages = numberOfPages;
}
paperback = isItPaperback;
if (suggestedRetailPrice <= 0) {
suggestedRetailPrice = 0;
} else {
suggestedRetailPrice = theSuggestedRetailPrice;
}
}

/**
* @return title
*/
public String getTitle() {
return title;
}

/**
* @return author
*/
public String getAuthor() {
return author;
}

/**
* @return isbn
*/
public int getIsbn() {
return isbn;
}

/**
* @return pages
*/
public int getPages() {
return pages;
}

/**
* @return paperback
*/
public boolean getPaperback() {
return paperback;
}

/**
* @return suggestedRetailPrice
*/
public int getSuggestedRetailPrice() {
return suggestedRetailPrice;
}

/**
* title
*/
public void setTitle(String whatIsTitle) {
title = whatIsTitle;
}

/**
* author
*/
public void setAuthor(String whoIsAuthor) {
author = whoIsAuthor;
}

/**
* isbn code
*/
public void setIsbn(int isbnCode) {
isbn = isbnCode;
}

/**
* number of pages
*/
public void setPages(int numberOfPages) {
if (pages <= 0) {
pages = 0;
} else {
pages = numberOfPages;
}
}

/**
* is it paperback
*/
public void setPaperback(boolean isItPaperback) {
paperback = isItPaperback;
}

/**
* suggested retail price
*/
public void setSuggestedRetailPrice(int theSuggestedRetailPrice) {
if (suggestedRetailPrice <= 0) {
suggestedRetailPrice = 0;
} else {
suggestedRetailPrice = theSuggestedRetailPrice;
}
}

/**
* displays information
*/
public void displayBook() {
System.out.println("Title : " + title);
System.out.println("Author : " + author);
System.out.println("ISBN : " + isbn);
System.out.println("Pages : " + pages);
System.out.println("Paperback : " + paperback);
System.out.println("Suggested price : " + suggestedRetailPrice);
}
}

最佳答案

您在使用参数 numberOfPages 设置页面值之前检查页面值:

 title = whatIsTitle;
author = whoIsAuthor;
isbn = isbnCode;

// pages is still at its initialzed value of 0 here.
if ( pages <= 0 )
{
pages = 0;
}
else
{
pages = numberOfPages; // this will *never* be called
}

颠倒这个顺序。或者更好的是,检查参数值并使用它来设置您的页面值:

if (numberOfPages < 0) {
pages = 0;
} else {
pages = numberOfPages
}

对于您的第二个问题,创建一个名为 isPaperback 的字符串,如果平装书为真,则将其设置为输出字符串的"is"字符串,否则为“否”,然后在需要时显示该字符串。或者将您的 System.out.println("yes") 放入 if block 中以测试平装书的值(value)。

即,

if (paperback) {
System.out.println(...);
} else {
System.out.pringln(...);
}

关于java - (初学者 Java 问题)为什么这段代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698610/

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