gpt4 book ai didi

java - 用 BufferedReader 替换扫描仪

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:48 25 4
gpt4 key购买 nike

我有一个愚蠢的问题:如何在这种方法中用 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

Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.

In fact you can pass a BufferedReader to a scanner as the source of characters to parse.

这是可能的,但没有兴趣在这里使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com