我的程序运行并编译。但是,我没有得到所需的输出。它输出所有需要的信息,但它也打印出一些我不喜欢的东西。与赞助人,作者和书籍一起,印刷了很多真/假的陈述:
/*true (Current out print statement)
true
true
true
Name: Ken Lambert
Title: Cider House Rules
Author: John Irving
Title: The Perfect Storm
Author: Sebastian Junger
Title: The Illiad
Author: Homer
Title: Hamlet
Author: William Shakespeare
true
false
false
Name: Ken Lambert
Title: The Perfect Storm
Author: Sebastian Junger
Title: The Illiad
Author: Homer
Title: Hamlet
Author: William Shakespeare (Current Print Statement)
Name: Ken Lambert (Desired print statement)
Title: Cider House Rules
Author: John Irving
Title: The Perfect Storm
Author: Sebastian Junger
Title: The Illiad
Author: Homer
Title: Hamlet
Author: William Shakespeare
Name: Ken Lambert
Title: The Perfect Storm
Author: Sebastian Junger
Title: The Illiad
Author: Homer
Title: Hamlet
Author: William Shakespeare*/
public class Book
{
private String author, title;
public Book(String t, String a){
author = a;
title = t; }
public String getAuthor(){
return author; }
public String getTitle(){
return title; }
public String toString(){
return "Title: " + title + "\n" +
"Author: " + author; }
}
public class LibraryTester{
public static void main (String[] args){
Patron p = new Patron("Ken Lambert");
Book b1 = new Book("Cider House Rules", "John Irving");
Book b2 = new Book("The Perfect Storm", "Sebastian Junger");
Book b3 = new Book("The Illiad", "Homer");
Book b4 = new Book("Hamlet", "William Shakespeare");
System.out.println(p.borrowBook(b1));
System.out.println(p.borrowBook(b2));
System.out.println(p.borrowBook(b3));
System.out.println(p.borrowBook(b4));
System.out.println(p);
System.out.println(p.returnBook("Cider House Rules"));
System.out.println(p.hasBook("Cider House Rules"));
System.out.println(p.hasBook("The Perfect Storm"));
System.out.println(p);
}
}
public class Patron{
Book b1, b2, b3, b4;
private String name;
public Patron(String nm){
name = nm;
b1 = null;
b2 = null;
b3 = null;
b4 = null;
}
public String getName(String nm){
if(name.equals(null))
System.out.println("You must put in your name!");
else
name = nm;
return name;
}
public boolean borrowBook(Book bbook){
boolean test = false;
if (b1 == null){
b1 = bbook;
test = true;
}
else if (b2 == null){
b2 = bbook;
test = true;
}
else if(b3 == null){
b3 = bbook;
test = true;
}
else if(b4 == null){
b4 = bbook;
test = true;
}
else
{
test = false;
}
return test;
}
public boolean hasBook(String title){
if(b1 != null)
if(b1.getTitle().equals(title)){
return true;
}
else if(b2 != null)
if(b2.getTitle().equals(title)){
return true;
}
else if(b3 != null)
if(b3.getTitle().equals(title)){
return true;
}
else if(b4 != null)
if(b4.getTitle().equals(title)){
return true;
}
return false;
}
public boolean returnBook(String b){
if(b1.getTitle().equals(b)){
b1 = null;
return true;
}
else if(b2.getTitle().equals(b)){
b2 = null;
return true;
}
else if(b3.getTitle().equals(b)){
b3 = null;
return true;
}
else if (b4.getTitle().equals(b)){
b4 = null;
return true;
}
else
{
return false;
}
}
public String toString(){
String str = "Name: " + name;
if(b1 != null)
str = str + "\n" + b1;
if(b2 != null)
str = str + "\n" + b2;
if(b3 != null)
str = str + "\n" + b3;
if(b4 != null)
str = str + "\n" + b4;
return str;
}
}//class
答案 0 :(得分:1)
方法public boolean borrowBook(Book bbook)
返回一个布尔值。由于你打印了方法的返回,它会打印出......布尔!
System.out.println(p.borrowBook(b1));
System.out.println(p.borrowBook(b2));
System.out.println(p.borrowBook(b3));
System.out.println(p.borrowBook(b4));
答案 1 :(得分:-1)
它打印所有这些" True / False" s因为您使用的方法返回布尔值。