我想知道如何使用JAva的Scanner类读取包含多个String单词的行。
There is no specific way to do that. Anyway you can provide comma separated strings as below example.
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] strArr = str.split(",");
for(String s: strArr){
System.out.println(s);
}
}
write a method like this:
function String reader(Scanner s, int lines){
String ret = "";
for(int i = 0; i < lines; i++)
ret += s.nextLine();
return ret;
}
and use it like this:
Scanner s = new Scanner(System.in);
String threeLines = reader(s, 3);
It's just a for loop that loops and read multiple lines
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.