2 回答
TA贡献1877条经验 获得超1个赞
我认为以下是您正在寻找的内容。对您的程序做了一些小改动。你真的不需要线路input.nextLine()。希望能帮助到你
输入示例
3 2
9 5 2
Good Luck
程序
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n= input.nextInt();
int[] lines = new int[m];
for (int i = 0; i < m; i++) {
lines[i] = input.nextInt();
}
String[] words = new String[n];
for (int i = 0; i < n; i++) {
words[i] = input.next();
}
TA贡献1802条经验 获得超5个赞
我让用户在这里一次插入一行输入。我不确定你在寻找什么,但这就是我所拥有的:
程序:
Scanner input = new Scanner(System.in);
System.out.print("Enter line 1: ");
int m = input.nextInt();
int n = input.nextInt();
System.out.println(m + " ints and " + n + " words");
System.out.print("Enter line 2: ");
int[] lines = new int[m];
String[] words = new String[n];
input.nextLine();
for (int i = 0; i < m; i++) {
lines[i] = input.nextInt();
}
System.out.print("Enter line 3: ");
for (int i = 0; i < n; i++) {
words[i] = input.next();
}
System.out.println(Arrays.toString(lines));
System.out.println(Arrays.toString(words));
安慰:
Enter line 1: 3 4
3 ints and 4 words
Enter line 2: 1 2 3
Enter line 3: four words right here
[1, 2, 3]
[four, words, right, here]
添加回答
举报