在 JAVA 中获取和 return 用户输入比 util.Scanner 更好的选择
Better options than util.Scanner to get and return user input in JAVA
所以我花了一些时间研究 util.Scanner 感谢这里的一些反馈。在 How to use java.util.Scanner to correctly...
上的 post 中找到了最让我印象深刻的陈述
You will probably never actually see Scanner used in professional/commercial line of business apps because everything it does is done better by something else. Real world software has to be more resilient and maintainable than Scanner allows you to write code. Real world software uses standardized file format parsers and documented file formats, not the adhoc input formats that you are given in stand alone assignments.
因此,我查找了其他一些获取用户输入的选项,并决定使用 BufferedReader。我想要完成的是让用户告诉程序他们是否愿意继续玩游戏,让程序重复问题直到给出有效输入 (Y/N),并让程序根据答案继续或终止。
三个相关问题:
BufferedReader 是否比 Scanner 更好?
是否有更好地解决此任务的选项?如果是的话,那是什么?
如果答案不符合标准,让方法调用自身来创建 "loop" 是否合适?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RageGame {
public static void main(String[] args) throws IOException {
char playAgain = 'Y';
// Loop game until user decides they are done.
do {
playAgain = playRage();
} while (playAgain == 'Y');
//Quit message
System.out.println("Thanks for playing Rage!");
}
public static char playRage() throws IOException {
return playAgain();
}
public static char playAgain() throws IOException{
char answer;
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
//Ask the user if they want to continue and set their response to upper case.
System.out.println("Do you wish to play again? (y/n)");
answer = (char) Character.toUpperCase(kb.read());
//Check if answer fits criteria
if (answer != 'Y' && answer != 'N') {
System.out.println("Sorry, " + answer + " is not a valid answer.");
return playAgain();
}else {
return answer;
}
}
}
简短的回答是:不要关闭它。
您的 Scanner
正在使用流 System.in
。 System.in
不是您的程序获取的资源(您没有打开流),因此 您 不应该关闭它。其他人负责关闭它。
如果您觉得警告很烦人,您可以将其静音。
假设您的扫描仪从文件而不是 System.in
获取输入,那么您需要在上次需要后调用 close
:
answer = Character.toUpperCase(kb.next().charAt(0));
kb.close();
如果你怕忘记调用close
,就用try-with-resources
块,它会帮你关闭!
try (Scanner kb = new Scanner(some other source)) {
System.out.println("Do you wish to play again? (y/n)");
answer = Character.toUpperCase(kb.next().charAt(0));
} // this will always close the scanner
换句话说,如果您想关闭扫描仪,可以将 System.in
包裹在 CloseShieldInputStream
中。您的扫描仪将使用 Proxy Stream。如果您有兴趣,请查看 http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html
所以我花了一些时间研究 util.Scanner 感谢这里的一些反馈。在 How to use java.util.Scanner to correctly...
上的 post 中找到了最让我印象深刻的陈述You will probably never actually see Scanner used in professional/commercial line of business apps because everything it does is done better by something else. Real world software has to be more resilient and maintainable than Scanner allows you to write code. Real world software uses standardized file format parsers and documented file formats, not the adhoc input formats that you are given in stand alone assignments.
因此,我查找了其他一些获取用户输入的选项,并决定使用 BufferedReader。我想要完成的是让用户告诉程序他们是否愿意继续玩游戏,让程序重复问题直到给出有效输入 (Y/N),并让程序根据答案继续或终止。
三个相关问题:
BufferedReader 是否比 Scanner 更好?
是否有更好地解决此任务的选项?如果是的话,那是什么?
如果答案不符合标准,让方法调用自身来创建 "loop" 是否合适?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RageGame {
public static void main(String[] args) throws IOException {
char playAgain = 'Y';
// Loop game until user decides they are done.
do {
playAgain = playRage();
} while (playAgain == 'Y');
//Quit message
System.out.println("Thanks for playing Rage!");
}
public static char playRage() throws IOException {
return playAgain();
}
public static char playAgain() throws IOException{
char answer;
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
//Ask the user if they want to continue and set their response to upper case.
System.out.println("Do you wish to play again? (y/n)");
answer = (char) Character.toUpperCase(kb.read());
//Check if answer fits criteria
if (answer != 'Y' && answer != 'N') {
System.out.println("Sorry, " + answer + " is not a valid answer.");
return playAgain();
}else {
return answer;
}
}
}
简短的回答是:不要关闭它。
您的 Scanner
正在使用流 System.in
。 System.in
不是您的程序获取的资源(您没有打开流),因此 您 不应该关闭它。其他人负责关闭它。
如果您觉得警告很烦人,您可以将其静音。
假设您的扫描仪从文件而不是 System.in
获取输入,那么您需要在上次需要后调用 close
:
answer = Character.toUpperCase(kb.next().charAt(0));
kb.close();
如果你怕忘记调用close
,就用try-with-resources
块,它会帮你关闭!
try (Scanner kb = new Scanner(some other source)) {
System.out.println("Do you wish to play again? (y/n)");
answer = Character.toUpperCase(kb.next().charAt(0));
} // this will always close the scanner
换句话说,如果您想关闭扫描仪,可以将 System.in
包裹在 CloseShieldInputStream
中。您的扫描仪将使用 Proxy Stream。如果您有兴趣,请查看 http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/CloseShieldInputStream.html