Java 布尔返回语句(使其不打印为真/假)
我的程序运行并编译。但是,我没有得到所需的输出。它输出所有需要的信息,但也打印出一些我不喜欢的东西。除了赞助人、作者和书籍外,还印刷了许多真假陈述:Java 布尔返回语句(使其不打印为真/假),java,boolean,Java,Boolean,我的程序运行并编译。但是,我没有得到所需的输出。它输出所有需要的信息,但也打印出一些我不喜欢的东西。除了赞助人、作者和书籍外,还印刷了许多真假陈述: /*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:
/*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
方法
publicboolboolbook(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));
它会打印所有这些“真/假”,因为您使用的方法返回布尔值。这是因为您会打印这些值。你怎么看
System.out.println(p.brookbook(b1))代码>是什么?它打印“真”或“假”。hasBook和borrowBook返回布尔值。如果调用System.out.println(true),您将打印出“true”。您可以使用类似System.out.println(p.hasBook(“完美风暴”)代码>并且想知道为什么会得到真/假输出?