Java同时打印两行

标签 java java.util.scanner

我正在尝试在屏幕上打印一条消息,然后从键盘获取一个值。我连续打印了 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/

相关文章:

java - 方法(do 循环)中的扫描仪不接受大于/小于符号

java - 如何生成空bpm :comment box in Alfresco Activiti Workflow Task?

java - 在 spring boot 应用程序中安排多个批处理作业

java - #{} ${} 和 %{} 有什么区别?

java - Groovy 使用反射读取特征方法

java - NoSuchElementException : No line found (using hasNextLine()

Java/返回动态对象类型

java - 标记包含空标记的字符串

java - 获取扫描仪类当前使用的分隔符

java - Java的Scanner类中的多个分隔符