java如何输出两行,Java一起打印两行

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:

Thanks in advance for your help!

解决方案

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

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.

To fix this problem, insert

key.nextLine();

in front of the switch statement.

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值