我正在尝试在屏幕上打印一条消息,然后从键盘获取一个值。我连续打印了 4 张,但它们之间有扫描方法。当我运行我的代码时,前两个打印一起运行,我无法在第一次打印后在第一个变量中插入值。
case 1:
System.out.println("###Book Data###");
System.out.print("Name of the book:\t");
String Name = key.nextLine();
System.out.print("ISBN of the book:\t");
String ISBN = key.nextLine();
System.out.print("Author of the book:\t");
String author = key.nextLine();
System.out.print("Copies of the book:\t");
int copies = key.nextInt();
book Book = new book(ISBN,Name,author,copies);
lib.AddBook(Book);
break;
#########Text Printed######
Please enter your selection: 1
###Book Data###
Name of the book: ISBN of the book:
预先感谢您的帮助!
最佳答案
println 会换行而 print 不会。您应该考虑使用 println、刷新缓冲区或调用换行转义字符“\n”
关于Java同时打印两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23301754/