即使我正确声明了变量,也不会创建变量

variables are not created even though I declared them Correctly

我正在尝试创建一个能够向学生发放书籍的图书馆管理系统。
书中应包含如下信息:
1.书名
2. 书籍作者
3. 发给
4. 发布于
用户应该可以添加图书,return 发行图书,发行图书

当我执行以下代码并选择选项 1 作为选项时,我无法为 bName 字符串和作者字符串提供输入

我做错了什么吗,我是新手 java 请帮助我

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

/*
Creating a library management system which is capable of issuing books to the students.
Book should have info like:
1. Book name
2. Book Author
3. Issued to
4. Issued on
User should be able to add books, return issued books, issue books
 */
class BookInfo
{
    protected String name,author,issuedTo,issuedOn;
    public BookInfo(String bName,String bAuthor) {
        this.addBook(bName,bAuthor);
    }
    public String getBookName()
    {
        return name;
    }
    public String getAuthor()
    {
        return author;
    }
    public String getIssuedOn()
    {
        return issuedOn;
    }
    public String getIssuedTo()
    {
        return issuedTo;
    }
    public void addBook(String name,String author)
    {
       this.name=name;
       this.author=author;
    }
    public void issueBook(String name)
    {
        this.issuedTo=name;
        LocalDateTime t=LocalDateTime.now();
        DateTimeFormatter dTF=DateTimeFormatter.ofPattern("dd/MMM/yyyy       HH:mm:ss");
        this.issuedOn=t.format(dTF);
    }
  public void returnIssuedBooks()
  {
      issuedTo=null;
      issuedOn=null;
  }

}

public class Main {

    public static void main(String[] args){

        ArrayList<BookInfo> l1 = new ArrayList<>();
        int choice;
        String bName;
        String name;
        String author;
        boolean isTrue = true;
        Scanner obj = new Scanner(System.in);
        System.out.println("welcome to the Library Management System");
            while (isTrue) {
                System.out.println("choose from the options below :");
                System.out.println("press 1 for adding the book");
                System.out.println("press 2 for issuing the book");
                System.out.println("press 3 for returning the issued book");
                System.out.println("press 4 to exit");
                choice = obj.nextInt();
                try {
                    switch (choice) {
                        case 1:
                            System.out.println("enter the name of the book :");
                            bName = obj.nextLine();
                            System.out.println("enter the author of the book");
                            author = obj.nextLine();
                            l1.add(new BookInfo(bName,author));
                            break;

                        case 2:
                            System.out.println("enter the name of the book to issue");
                            bName = obj.nextLine();
                            System.out.println("enter your name :");
                            name = obj.nextLine();
                            for (BookInfo obj2 : l1) {
                                if (obj2.getBookName().equals(bName)) {
                                    obj2.issueBook(name);
                                }
                            }
                            break;

                        case 3:
                            System.out.println("enter the name of the book you want to return");
                            bName = obj.nextLine();
                            for (BookInfo obj2 : l1) {
                                if (obj2.getBookName().equals(bName)) {
                                    obj2.returnIssuedBooks();
                                }
                            }
                            break;

                        case 4:
                            isTrue = false;
                            break;

                        default:
                            throw new Exception();
                    }
                }catch(Exception e)
                {
                    System.out.println("enter the correct input");
                }
            }
    }
    }

这是下面的输出

welcome to the Library Management System
    choose from the options below :
    press 1 for adding the book
    press 2 for issuing the book
    press 3 for returning the issued book
    press 4 to exit
    1
    enter the name of the book :
    enter the author of the book 

那是因为 Scanner.nextInt 方法不会读取您通过按 "Enter" 创建的输入中的换行符因此在阅读该换行符后调用 Scanner.nextLine returns 。 有两个选项可以解决此问题,

1.通过 Scanner.nextLine 读取输入并将您的输入转换为您需要的正确格式

    Scanner obj = new Scanner(System.in);
    int choice = Integer.parseInt(obj .nextLine());
    

2。在每个 Scanner.nextInt

之后添加 Scanner.nextLine 调用
    Scanner obj = new Scanner(System.in);
    int choice = = obj.nextInt();
    obj.nextLine();