作为我的第一个Java项目之一,我想构建抵押贷款计算器。我试图限制用户可以使用此代码选择的持续时间]
Scanner numOfPayments = new Scanner(System.in);
while (true) {
System.out.print("Enter number of payments (the number of Years you will be paying the loan):");
short numOfPayments2 = numOfPayments.nextShort();
if (numOfPayments2 >= 1 && numOfPayments3 <= 30) {
numOfPayments3 = (short) (numOfPayments2 * 12);
break;
}
System.out.println("Enter a valid number of years(Between 1 and 30)");
}
它可以限制其他值,但不适用于此值。在此先感谢
感谢@azro的回答我应该在If语句中将numOfPayments3更改为numOfPayments2