更新 当用户第一次做出诸如“1”之类的选择时,菜单会再次显示。下一次做出选择时,支付信息开始循环。循环完成并再次显示菜单后,它可以正常工作。此外,前两年是输出,而不是仅在选择开始循环时输出第一年,然后按预期一次输出一年。
//create scanner object for choosing a loan, then prompt for and accept input
Scanner choose = new Scanner(System.in);
String choice;
System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
choice = choose.next();
//cycle loan 1 payment information
//create scanner object to advance to the next year's payments
//loop for cycling payment information
//initialize loan principal to variable
while (!"end".equals(choice)) {
System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
choice = null;
choice = choose.next();
if ("1".equals(choice)) {
//calculation code
}
if (j < 6) {
System.out.println("Press enter to get the mortgage information for year " + (j + 2));
choice = choose.nextLine();
} else {
System.out.println("Congratulations, your mortgage has been paid off.");
}
}
choice = null;
}
if ("2".equals(choice)) {
//calculation code
}
if (j < 14) {
System.out.println("Press enter to get the mortgage information for year " + (j + 2));
choice = choose.nextLine();
} else {
System.out.println("Congratulations, your mortgage has been paid off.");
}
}
choice = null;
}
if ("3".equals(choice)) {
//calculation code
}
if (j < 29) {
System.out.println("Press enter to get the mortgage information for year " + (j + 2));
choice = next.nextLine();
} else {
System.out.println("Congratulations, your mortgage has been paid off.");
}
}
choice = null;
}
}
choose.close();
}
}