我在两种方法中遇到错误,public int getBookIndex
和public boolean updateTitle
。
/ d / 1r4XAhOC0VvWfaxnPZUFyiSGdk2gpFs2mZoTkJ6CJnGk /编辑?USP =共享
public class Library {
private String[] titles ;
private int[] nbPages;
private int nb;
private String[] author;
public Library(int total_books) {
nb = total_books;
titles = new String[nb];
nbPages = new int[nb];
author = new String[nb];
}
public void setDate(String tit, int nbP, String au) {
for(int x=0; x<=nb; x++){
titles[x] = tit;
author[x] = au;
nbPages[x] = nbP;
}
}
public void DisplayAll() {
for(int x=0;x<=nb;x++) {
System.out.println(titles[x] + nbPages[x] + author[x]);
}
}
public void displayBook(String title){
for(int x=0;x<=nb;x++)
if(title == titles[x]){
System.out.println(titles[x] + author[x] + nbPages[x]);
break;
}
}
public void displayBook(int number){
System.out.println(titles[number] + author[number]);
}
public void displayAuthorBooks(String auth) {
for(int x=0;x<=nb;x++){
if(auth == author[x])
System.out.print(titles[x]+nbPages[x]+author[x]);
}
}
public void displayNumberOfBooksAuthor(String auth){
int count = 0;
for(int x=0;x<=nb;x++){
if(auth == author[x])
count++;
}
System.out.println("the books for this author is :" + count);
}
public boolean addBook(String titl, int nbp) {
nb++;
titles[nb] = titl;
nbPages[nb] = nbp;
return true ;
}
public boolean deleteBook(int index){
if(index <= nb) {
nb--;
return true;
}
else
return false ;
}
public boolean updateTitle(int index,String tit) {
if((titles[index] == tit) == true)
return true;
else
return false;
}
public int getBookIndex(String find_tit){
for(int x=0;x<=nb;x++){
if(titles[x] == find_tit){
return (x++);
break;
}
else
return(-1);
}
}
public boolean updateTitle(String old_titl,String new_titl) {
for(int x=0;x<=nb;x++){
if(titles[x] == old_titl) {
titles[x] = new_titl;
return true;
break;
}
else
return false;
}
}
}
答案 0 :(得分:0)
删除你的break语句(你的其他条件也是错误的) - 就像这样
public int getBookIndex(String find_tit) {
for (int x = 0; x < nb; x++) { // not sure, but probably not <=
if (titles[x].equals(find_tit)) { // not ==
return x; // probably not x+1.
}
}
return (-1);
}
public boolean updateTitle(String old_titl,
String new_titl) {
for (int x = 0; x < nb; x++) { // not sure, but probably not <=
if (titles[x].equals(old_titl)) { // not ==
titles[x] = new_titl;
return true;
}
}
return false;
}