我有一个愚蠢的问题:在这种方法中,如何用BufferedReader替换Scanner:
public Coordinates getShot() {
Scanner scanner = new Scanner(System.in);
int x,y;
do {
System.out.print("Enter Х: ");
x = Integer.parseInt(scanner.nextLine());
System.out.print("Enter Y: ");
y = Integer.parseInt(scanner.nextLine());
System.out.printf("\nYou entered: [%d,%d]\n", x, y);
if(!Coordinates.checkCoordinates(x, y))
System.out.println("Error");
} while(!Coordinates.checkCoordinates(x ,y));
Coordinates shot = new Coordinates(x, y);
return shot;
}
最佳答案
见Scanner vs. BufferedReader
扫描器用于从流的内容中解析令牌,而BufferedReader只是读取流,并且不执行任何特殊的解析。
实际上,您可以将BufferedReader传递给扫描仪作为要解析的字符源。
有可能,但是在这里使用BufferedReader不会引起兴趣:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//....
int x,y;
System.out.print("Enter Х: ");
x = Integer.parseInt(reader.readLine());
System.out.print("Enter Y: ");
y = Integer.parseInt(reader.readLine());
System.out.printf("\nYou entered: [%d,%d]\n", x, y);
关于java - 用BufferedReader替换扫描仪,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36551696/