关于java输入易错点
nextLine()自动读取了被next()去掉的Enter作为它的结束符,所以没办法给s2从键盘输入值。经过验证,发现其他的next的方法,如nextDouble() ,nextFloat() ,nextInt() 等与nextLine()连用时都存在这个问题,解决的办法是:在每一个 next()、nextDouble() 、 nextFloat()、nextInt() 等语句之后加一个nextLine()语句,将被next()去掉的Enter等结束符过滤掉。
public class NextTest{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个字符串:");
String s1 = input.next();
input.nextLine(); //接收结束符
System.out.print("请输入第二个字符串:");
String s2 = input.nextLine();
System.out.println("输入的字符串是:"+ s1 + " " + s2);
}
}
———————————————— 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_43895371/article/details/109013500