Java中.next()和.nextLine()的区别

版权声明:本文为博主原创文章,转载请附上博文链接 https://blog.csdn.net/weixin_40807247/article/details/89022087
package com.huawei;

import java.util.Scanner;

public class Main1 {
 
	public static void main(String[] args) {
		
		String s1,s2;  
		
		Scanner sc=new Scanner(System.in);  
		System.out.print("请输入第一个字符串:");  
		//nextLine()方法的结束符只是Enter键
		s1=sc.nextLine(); 
		System.out.println("输入的字符串是:"+s1);
		
		System.out.print("请输入第二个字符串:"); 
		//next()在输入有效字符之后,将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符
		s2=sc.next(); 
		System.out.println("输入的字符串是:"+s2);
		
		}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40807247/article/details/89022087