Java一起打印两行 [英] Java print two lines together

查看:89
本文介绍了Java一起打印两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在屏幕上打印一条消息,然后从键盘上获取一个值.我连续打印4张照片,但它们之间有扫描方法.当我运行代码时,前两次打印会同时运行,并且在第一次打印之后,我无法在第一个变量中插入值.

I'm trying to print a message on the screen and after that, take a value from the keyboard. i have 4 prints in a row but i have scan methods between them. When i run my code, the first two prints run together and i can't insert a value at the first variable, after the first print.

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:

提前感谢您的帮助!

推荐答案

这是因为switch语句上方的行具有key.nextInt() * .

This is because the line above your switch statement has key.nextInt() *.

扫描程序读取整数,但是它将行尾字符'\n'留在缓冲区中.在switch语句内的key.nextLine();向您返回一些相关数据之前,您需要以某种方式消耗该'\n'字符.

The scanner reads the integer, but it leaves the end-of-line character '\n' in the buffer. You need to consume that '\n' character somehow, before the key.nextLine(); inside the switch statement returns some relevant data to you.

要解决此问题,请插入

key.nextLine();

switch语句之前.

* 不要问我我怎么知道:-)

* don't ask me how I know that :-)

这篇关于Java一起打印两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆