而无限(限制)循环无法正常工作[关闭]

问题描述 投票:0回答:1

作为我的第一个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)");
}

它可以限制其他值,但不适用于此值。在此先感谢

java while-loop infinite-loop
1个回答
0
投票

感谢@azro的回答我应该在If语句中将numOfPayments3更改为numOfPayments2

© www.soinside.com 2019 - 2024. All rights reserved.