How to keep my user input on the same line after an output?

♀尐吖头ヾ 提交于 2019-12-17 14:56:33

问题


I'm trying to write code that asks for the users age and then they enter it, but I want the number to appear next to the question after you enter it.

My code looks like this:

System.out.println("Enter a number: ");
num1 = userIn.nextInt();

It works fine, but the number always appears on the line below.

Output:

Enter a number:
12

What I want:

Enter a number: 12

Any suggestions?


回答1:


Use print() instead of println();

System.out.println() automatically adds a newline character. That what the ln means.




回答2:


System.out.print instead of println.

println adds a newline after it prints your output, print will just print your message.




回答3:


Because you used System.out.println which adds a new-line (\n) after the output. Use System.out.print instead.




回答4:


Use System.out.print() instead of println. Println adds a new line after the arguments inside the parameter has been passed.



来源:https://stackoverflow.com/questions/10148573/how-to-keep-my-user-input-on-the-same-line-after-an-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!