import java.util.Scanner
public class Editable {
/* Return true if amount is within MIN_AMOUNT and MAX_AMOUNT
*/
public static boolean isValidAmount(double amount) {
return amount > MIN_AMOUNT && amount < MAX_AMOUNT;
}
/* Asks user to input amount until isValidAmount is true.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(system.in);
}
我是Java的新手,我一直在爲此苦苦掙扎。 我要求在主要用戶輸入,我需要它繼續詢問用戶輸入,直到isValidAmount返回true。我已經嘗試了多種不同的解決方案,但似乎無法使其發揮作用。如何使用不同的方法驗證掃描儀輸入?
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double inputAmount = 0;
try {
while (!isValidAmount(inputAmount)) {
System.out.println("Enter amount:");
inputAmount = scanner.nextDouble();
scanner.next();
}
} catch (Exception e) {
System.out.println("Input error.");
scanner.next();
}
您能否詳細說明「似乎無法使其工作」的含義? –