Scanner中next和nextLine的区别:
next:每次读取一个单词,以空格或者回车作为分隔符
nextLine:每次读取一行,以回车作为分隔符
package com.soldier.test;
import java.util.Scanner;
public class Exercise {
public static void main(String[] args) {
System.out.println("How old are you?");
Scanner sc = new Scanner(System.in);
// 输入: I am 23 years old
String age = sc.next();
System.out.println("age = " + age); // 输出:I
Scanner ss = new Scanner(System.in);
// 输入: I am 23 years old
String age2 = ss.nextLine();
System.out.println("age2 = " + age2); // 输出:I am 23 years old
}
}
本文来自博客园,作者:寒露凝珠,转载请注明原文链接:https://www.cnblogs.com/china-soldier/p/15249438.html