2015-12-11 184 views
0

上遇到此方法有问题。任何人都可以发现明显的错误?当我运行该程序并要求我输入要编辑的ID时,我输入了一个正确的ID,但它说“此ID已存在”..我只想说在编辑本书的ID后,如果说得通。任何帮助非常感谢。图书馆应用程序。在编辑书籍方法

public void editBook(ArrayList<Book> books) { 

    boolean exsistingID = false; 
    int editID, ID; 
    String newTitle, newAuthor, newPublisher; 
    int newReleaseYear; 
    //int newNumLoans; 
    //boolean newOnLoan; 

    do { 
     System.out.println("Enter the ID of the book you wish to edit: "); 
     editID = scan.nextInt(); 
     scan.nextLine(); 
     if (editID == 0) { 
      System.out.println("ERROR - Book ID can not be 0. Please try again"); 
     } 
    } while (editID == 0); 

    for (int a = 0; a < books.size(); a++) { 
     Book z = books.get(a); 
     if (editID == (z.getBookID())) { 
      exsistingID = true; 
      break; 
     } 
    } 

    if (exsistingID == true) { 
     System.out.println("This ID already exists. Please try again"); 
     do { 
      System.out.println("Enter a new ID: "); 
      ID = scan.nextInt(); 
      scan.nextLine(); 
      if (ID == 0) { 
       System.out.println("ERROR - Book ID can not be 0. Please try again"); 
      } 
     } while (ID == 0); 

回答

1

据我理解你的代码,你想你刚才什么都行

System.out.println("This ID already exists. Please try again"); 

在你的代码为时尚早。 你说你想在你编辑书本编号后打印这条信息,但是这在代码中还没有完成。 (也许你想要显示这条消息,如果在编辑本书的ID之前已经存在新的ID)

最后一个do-while循环似乎检查新的id是否已经存在。您可以使用一个新的布尔(如你检查的第一个ID是否以同样的方式),以检查新的目标-ID存在,如果这样做,你可以使用线

System.out.println("This ID already exists. Please try again"); 

在你的代码和你程序应该按照你的意愿行事。

0

您指定的真值,环路上的exsistingID变量,你有这样明显的代码运行巫时间evrey你输入有效的身份证件

if (exsistingID == true) { 
    System.out.println("This ID already exists. Please try again");