Java 扫描仪在这种情况下如何工作?
How does Java Scanner work in this situation?
我有一个任务。
在控制台输入数据。
第一行包含列表的元素。第二行包含掉期数。然后按照对换的描述行。每行包含两个数字:交换元素的索引。
Example:
Input:
1 2 3 4 5 6
2
0 1
3 5
Output: 2 1 3 6 5 4
问题是如何正确读取2个数字。 scanner.nextInt() 只读取 1 个整数。想法是将它们作为字符串读取,然后在数组中拆分并作为整数传递给 swap 方法。它不能正常工作。
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt();
for (int i = 0; i < swapNum; i++) {
scanner.nextLine();
String[] sw = scanner.nextLine().split(" ");
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
for (String item : list) {
System.out.print(item + " ");
}
}
}
我看了答案
for (int i = 0; i < swapNum; i++) {
Collections.swap(list, scanner.nextInt(), scanner.nextInt());
}
为什么它在我的解决方案中不能正常工作?
为什么在答案中正确读取了两个整数?
Scanner.nextLine() -> 将读取所有剩余行并 return 它。之后它将无法再在扫描仪中使用。
Scanner.nextInt() -> 将读取下一个整数并使用它。之后它将无法再在扫描仪中使用。
在您的解决方案中,您一行阅读了两次。
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); <-- you read first line
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt(); <-- you read first int of second line but you have not consumed all the line. So next time you will read again from second line
for (int i = 0; i < swapNum; i++) {
scanner.nextLine(); <-- Once you read it here it is consumed
String[] sw = scanner.nextLine().split(" "); <---here it will read
the next line the second time you call it, not the same result as you had in the
previous line of code.
The result of previous line of code is now gone! You have skipped this line.
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
试试
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < swapNum; i++) {
String readedLine = scanner.nextLine();
String[] sw = readedLine.split(" ");
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
甚至更好为什么你不尝试使用 nextLine() 清理读取每一行并用它来制作你想要的一切,而不是到处使用 nextInt()?
String inputLine = scaner.next(); //will read all the line
String[] numbersAsStrings = inputLine.split("\s+"); //split input line with empty space
List<Integer> numbersAsInt = new ArrayList<>();
Arrays.stream(numbersAsStrings).mapToInt(Integer::valueOf).forEach(numbersAsInt::add);//collect all integers
似乎在您输入交换次数后,扫描器使用 nextInt()
方法只获取部分行并将其解析为整数。此行的其余部分被循环中的 scanner.nextLine()
使用,而循环恰好是空的。这就是你收到错误的原因。快速解决方法是使用整行并将其解析为交换次数:
int swapNum = Integer.parseInt(scanner.nextLine());
同样如@Turamarth 所述,删除循环中的第一行,因为它会提示您输入两次:
scanner.nextLine(); <- REMOVE
String[] sw = scanner.nextLine().split(" ");
如果有帮助请告诉我
我有一个任务。 在控制台输入数据。 第一行包含列表的元素。第二行包含掉期数。然后按照对换的描述行。每行包含两个数字:交换元素的索引。
Example:
Input:
1 2 3 4 5 6
2
0 1
3 5
Output: 2 1 3 6 5 4
问题是如何正确读取2个数字。 scanner.nextInt() 只读取 1 个整数。想法是将它们作为字符串读取,然后在数组中拆分并作为整数传递给 swap 方法。它不能正常工作。
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt();
for (int i = 0; i < swapNum; i++) {
scanner.nextLine();
String[] sw = scanner.nextLine().split(" ");
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
for (String item : list) {
System.out.print(item + " ");
}
}
}
我看了答案
for (int i = 0; i < swapNum; i++) {
Collections.swap(list, scanner.nextInt(), scanner.nextInt());
}
为什么它在我的解决方案中不能正常工作? 为什么在答案中正确读取了两个整数?
Scanner.nextLine() -> 将读取所有剩余行并 return 它。之后它将无法再在扫描仪中使用。
Scanner.nextInt() -> 将读取下一个整数并使用它。之后它将无法再在扫描仪中使用。
在您的解决方案中,您一行阅读了两次。
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine(); <-- you read first line
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt(); <-- you read first int of second line but you have not consumed all the line. So next time you will read again from second line
for (int i = 0; i < swapNum; i++) {
scanner.nextLine(); <-- Once you read it here it is consumed
String[] sw = scanner.nextLine().split(" "); <---here it will read
the next line the second time you call it, not the same result as you had in the
previous line of code.
The result of previous line of code is now gone! You have skipped this line.
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
试试
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
ArrayList<String> list = new ArrayList<>(Arrays.asList(input.split(" ")));
int swapNum = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < swapNum; i++) {
String readedLine = scanner.nextLine();
String[] sw = readedLine.split(" ");
int f = Integer.parseInt(sw[0]);
int s = Integer.parseInt(sw[1]);
Collections.swap(list, f, s);
}
甚至更好为什么你不尝试使用 nextLine() 清理读取每一行并用它来制作你想要的一切,而不是到处使用 nextInt()?
String inputLine = scaner.next(); //will read all the line
String[] numbersAsStrings = inputLine.split("\s+"); //split input line with empty space
List<Integer> numbersAsInt = new ArrayList<>();
Arrays.stream(numbersAsStrings).mapToInt(Integer::valueOf).forEach(numbersAsInt::add);//collect all integers
似乎在您输入交换次数后,扫描器使用 nextInt()
方法只获取部分行并将其解析为整数。此行的其余部分被循环中的 scanner.nextLine()
使用,而循环恰好是空的。这就是你收到错误的原因。快速解决方法是使用整行并将其解析为交换次数:
int swapNum = Integer.parseInt(scanner.nextLine());
同样如@Turamarth 所述,删除循环中的第一行,因为它会提示您输入两次:
scanner.nextLine(); <- REMOVE
String[] sw = scanner.nextLine().split(" ");
如果有帮助请告诉我