我是java的新手,所以如果这听起来绝对愚蠢,请不要降价
好的,我如何使用单个扫描仪对象输入
5
你好,你好吗
欢迎来到我的世界
6 7
对于那些建议
的人scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,
检查出来,它不起作用!!!
感谢
答案 0 :(得分:20)
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please specify how many lines you want to enter: ");
String[] input = new String[in.nextInt()];
in.nextLine(); //consuming the <enter> from input above
for (int i = 0; i < input.length; i++) {
input[i] = in.nextLine();
}
System.out.printf("\nYour input:\n");
for (String s : input) {
System.out.println(s);
}
}
示例执行:
Please specify how many lines you want to enter: 3
Line1
Line2
Line3
Your input:
Line1
Line2
Line3
答案 1 :(得分:1)
public class Sol{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
System.out.println(sc.nextLine());
}
}
}
答案 2 :(得分:1)
默认情况下,扫描仪使用空格作为分隔符。为了使用forEachRemaining按行扫描,请如下将扫描仪定界符更改为line。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");
scanner.forEachRemaining(System.out::println);
}
答案 3 :(得分:0)
试试这段代码
Scanner in = new Scanner(System.in);
System.out.printf("xxxxxxxxxxxxxxx ");
String[] input = new String[in.nextInt()];
for (int i = 0; i < input.length; i++) {
input[i] = in.nextLine();
}
for (String s : input) {
System.out.println(s);
}
答案 4 :(得分:0)
您也只能尝试使用lambda:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.forEachRemaining(input -> System.out.println(input));
}
答案 5 :(得分:0)
也许我们可以使用这种方法:
for(int i = 0; i < n; i++)
{
Scanner sc1 = new Scanner(System.in);
str0[i] = sc1.nextLine();
System.out.println(str0[i]);
}
也就是说,我们每次读取nextLine之前都会创建扫描程序对象。 :)