[英]Is there a way to make this user validation code more efficient and/or easier to read?
我正在尝试验证用户输入,以便程序将循环回到第一个问题,如果它不是 int 并且 int 不在 9 - 12 的范围内,则询问用户的等级。 有没有“更好”的方式来编写这段代码?
do
{
if (userGrade < 9 || userGrade > 12)
{
System.out.println("That is not a valid grade!");
}
System.out.printf("Grade (9-12): ");
while(!enterInfo.hasNextInt())
{
System.out.println("That is not a number! Enter in a valid number.");
enterInfo.next();
}
userGrade = enterInfo.nextInt();
} while (userGrade < 9 || userGrade > 12);
为了使代码更清晰,您可以使用基于类和方法的封装(封装是 OOP 的主要原因)。
所以你尽可能把所有东西分成更小的部分作为方法或类,然后每个方法只有一个简单的目的。 这样整个程序更易于阅读、理解和维护。
例如,注意扫描器对象是如何在 readInput 方法的本地上下文中使用的。
import java.util.InputMismatchException;
import java.util.Scanner;
public class KillerLoop {
private boolean notReady;
private int grade;
public static void main(String[] args) {
new KillerLoop();
}
/**
* the default constructor calls the doStuff method
* which contains the main loop of the program
*/
public KillerLoop() {
this.notReady = true;
doStuff();
}
/**
* the programs main loop
*/
private void doStuff() {
while (this.notReady) {
int input = this.readInput();
this.verifyInput(input);
}
System.out.println("Grade " + this.grade + " is a correct grade!");
}
/**
* verifies a users input
* if the input is correct, notReady will be set
* to false so that the programs main loop is left
* (you could also use an if construct with break for this purpose)
* @param userGrade the users input
*/
private void verifyInput(int userGrade) {
if (userGrade < 9 || userGrade > 12) {
System.out.println("That is not a valid grade!\n" + "Grade (9-12): ");
} else {
this.grade = userGrade;
this.notReady = false;
}
}
/**
* this method reads input from the command line
* and returns an integer if successful
* @return the users input as integer
*/
private int readInput() {
Scanner scanner = new Scanner(System.in);
System.out.println("enter a grade");
int userGrade = 0;
try {
userGrade = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("That is not a number! Enter in a valid number.");
this.readInput(); //this recursion might not always be a good idea ;)
}
return userGrade;
}
}
基本上我会阅读 System.in 并且虽然有一些东西,但首先我会尝试转换为 Integer 然后检查该 Integer 是否在正确的范围内:
package trial;
import java.util.Scanner;
public class TestScan {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println("Please introduce a number:");
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String input=sc.next();
Integer inputInt;
try{
inputInt=Integer.parseInt(input);
}catch(Exception e){
System.out.println("You must introduce a number");
continue;
}
if(inputInt<9 || inputInt>12){
System.out.println("Number must be between 9 and 12 (inclusive)");
continue;
}
System.out.println("Correct!");
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
值得注意的是,由于从 System.in 中读取,该程序可以在您的 IDE 中执行,因此您必须在您的 IDE 之外执行它:
1.-转到TestScan文件夹并编译它:javac TestScan.java
2.- 在你的类补丁中指定这个类来执行它。 例如,如果你在 C 中:你可以使用类似的东西
C:>java -classpath C:\\workspace\\StackOverflow\\src trial.TestScan
if (userGrade < 9 || userGrade > 12)
{
System.out.println("That is not a valid grade!");
} else {
do {
System.out.printf("Grade (9-12):
");
while(!enterInfo.hasNextInt())
{
System.out.println("That is not a number! Enter in a valid number.");
enterInfo.next();
}
userGrade = enterInfo.nextInt();
} while (userGrade < 9 || userGrade > 12)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.