简体   繁体   English

java-从用户读取两个字符串

[英]java - reading two strings from the user

I'm trying to get the user to enter two strings and store them in two variables called studentName and studentNum , but when I run the program, the first string input is skipped over and only the second string input is allowed to be entered. 我试图让用户输入两个字符串并将它们存储在两个名为studentNamestudentNum变量中,但是当我运行该程序时,第一个字符串输入被跳过,仅第二个字符串输入被允许输入。 It prints out normally, but won't allow me to enter a value for studentName . 它可以正常打印,但不允许我输入studentName的值。 could anyone help me with this problem? 谁能帮助我解决这个问题?

System.out.print("Please enter your name: "); studentName = in.nextLine(); System.out.print("\\nPlease enter your student number: "); studentNum = in.nextLine();

Has to do with where the scanner starts and where it puts up a newline character 与扫描仪的启动位置以及放置换行符的位置有关

You can fix this if you change System.out.print to System.out.println 如果你改变你可以解决这个问题System.out.printSystem.out.println

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM