に私は、私はそれが両方であることを確認するには、ユーザからの入力を得る:複数の入力の検証の統合Javaの
- 数を最小値よりも
- 大きい
私は、次のコードを書きましたこれを達成するためには、それはされているよりも複雑なようです。 の質問を統合する方法はありますか?入力は数字で、その数字は10未満です、または同様の2つの部分の検証?
// function prompts user for a double greater than number passed in
// continues to prompt user until they input a number greater than
// the minimum number
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput;
System.out.print("Enter a number greater than " + minimumInput + ": ");
while (!scan.hasNextDouble()){
String garbage = scan.next();
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
} // end while
userInput = scan.nextDouble();
while (userInput <= minimumInput) {
System.out.println("\nInvalid input.\n");
userInput = getInput(minimumInput);
}
return userInput;
} // end getInput
を、それが多すぎるので、私はあなたの答えの最後の部分を削除することをお勧めその知識レベルの人のための情報。 – talex
そうでした。彼の質問の「スタイル」の側面にもっと焦点を合わせました。有効な提案、ありがとうございます! – GhostCat
フィードバックいただきありがとうございます! –