简体   繁体   English

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

[英](Beginner Java issues) Why isn't this code working?

(full code below) I have just handed in a lab for class where we had to make a class that describes a certain book. (下面的完整代码)我刚刚上课实验室,我们不得不上一个描述某本书的课程。 I could not figure out how to do two things. 我无法弄清楚如何做两件事。 1. If someone inputs a value less than zero for the 'pages' or 'suggestedRetailPrice' , the value must be set to zero. 1.如果有人为'pages'或'suggestedRetailPrice'输入小于零的值,则该值必须设置为零。 In this code the value is set 0 even if the value is positive. 在此代码中,即使值为正,该值也会设置为0。 In the: 在里面:


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

code if I set the second '0' to a different number, say: 如果我将第二个'0'设置为不同的数字,请说:


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

Then the value for whatever you input for 'pages' will be 1. But shouldn't it be 1 ONLY if the value you input is a negative number? 那么你为'pages'输入的任何值都是1.但是如果你输入的值是负数,它不应该只是1吗? I don't get what I'm doing wrong. 我不知道我做错了什么。

The second thing I could not figure out is at the bottom of the code, we had to display all info. 我无法弄清楚的第二件事是在代码的底部,我们必须显示所有信息。 My teacher wanted us to display whether or not the book was paperback as 'yes' or 'no' instead of 'true' or 'false'. 我的老师希望我们将这本书的平装本显示为“是”或“否”而不是“真实”或“假”。 How do I do this? 我该怎么做呢? I tried putting an if/else statement in like this: System.out.println("Paperback : " + if (paperback = true) {Yes} if (paperback = false) {no}; ) 我试着像这样放一个if / else语句:System.out.println(“平装:”+ if(平装=真){是} if(平装=假){无};)

Didn't work, can't figure it out. 没工作,无法搞清楚。 See whole code below. 请参阅下面的完整代码


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);
    }
}

You're checking the pages value before setting it with the parameter, numberOfPages: 您在使用参数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
 } 

Reverse this order. 颠倒这个顺序。 Or better, check the parameter value and use it to set your pages value: 或者更好,检查参数值并使用它来设置您的页面值:

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

For your second question, create a String, say called isPaperback, set it to "yes" String to your output String if paperback is true, and "no" if not, and then display that String when you need it. 对于你的第二个问题,创建一个String,比如名为isPaperback,将其设置为“yes”如果平装是真的话,将字符串设置为输出字符串,如果不是,则设置为“否”,然后在需要时显示该字符串。 Either that or put your System.out.println("yes") in an if block testing the value of paperback. 或者将你的System.out.println(“yes”)放在if块中测试平装的值。

ie,

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

I'm a little unsure what your trying to do but I notice in the Book class you are using "pages" when the parameter is "numberOfPages". 我有点不确定你想要做什么,但我注意到在Book类中,当参数为“numberOfPages”时,你正在使用“pages”。 Try doing 试着做

if(numberOfPages <= 0) pages = 0

Also, Bonus Points you can make this all neat and pretty if you can figure out how to use the ternary operator, but I'll leave that one up to you. 此外,如果你能弄清楚如何使用三元运算符,你可以使这一切变得整洁和漂亮,但我会把它留给你。

As for the print statement take the "if" outside of the print so it would be. 至于print语句,请在打印之外取“if”。

if(paperback) System.out.println("Paperback: Yes")
else System.out.println("Paperback: no)

Also look this is another great spot for the ternary operator. 另外看这是三元运营商的另一个好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM