用于菜单选择的Java-While循环(基于控制台的程序)
更新 用户第一次做出“1”等选择时,菜单将再次显示。下次选择时,付款信息开始循环。循环完成并再次显示菜单后,它将正常工作。此外,前两年是输出,而不仅仅是选择开始循环时的第一年,然后按计划一次输出一年用于菜单选择的Java-While循环(基于控制台的程序),java,Java,更新 用户第一次做出“1”等选择时,菜单将再次显示。下次选择时,付款信息开始循环。循环完成并再次显示菜单后,它将正常工作。此外,前两年是输出,而不仅仅是选择开始循环时的第一年,然后按计划一次输出一年 //create scanner object for choosing a loan, then prompt for and accept input Scanner choose = new Scanner(System.in); String choice; Syst
//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();
}
//创建用于选择贷款的扫描程序对象,然后提示并接受输入
扫描仪选择=新扫描仪(System.in);
字符串选择;
System.out.println(“\n键入1、2或3,然后按enter键查看相应贷款的每月付款信息。要结束程序,请键入\“end\”);
choice=choose.next();
//循环贷款1付款信息
//创建扫描仪对象以预付到下一年的付款
//循环支付信息的循环
//将贷款本金初始化为变量
而(!“结束”。等于(选择)){
System.out.println(“\n键入1、2或3,然后按enter键查看相应贷款的每月付款信息。要结束程序,请键入\“end\”);
choice=null;
choice=choose.next();
如果(“1”。等于(选择)){
//计算代码
}
if(j<6){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=choose.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
}
choice=null;
}
如果(“2”。等于(选择)){
//计算代码
}
如果(j<14){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=choose.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
}
choice=null;
}
如果(“3”。等于(选择)){
//计算代码
}
如果(j<29){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=next.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
}
choice=null;
}
}
选择。关闭();
}
}一个错误是字符串相等性与==不同。您应该使用.equals():
希望有帮助 您正在将
choice
设置为null
,因此choice!=“结束”
始终为真。
移动next.close();choice=null
以在while循环之外执行。
还有weolfe91所说的。我可以看到三个问题:
流中的系统不需要两个扫描仪。删除此语句Scanner next=new Scanner(System.in)
并使用选择
实例
next.close()的形式关闭输入扫描仪
,它将关闭您的输入流系统。在
中,您可能无法再次读取该流确保只有在完全完成程序后才能关闭流。equals
方法将while
中的条件与while(!“end”.equals(choice))
中的条件进行比较。将文本“end”
作为第一个参数,它将处理选项的空值
Scanner choose = new Scanner(System.in);
String choice= null;
int j = 0;
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 = choose.nextLine();
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)) {
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)) {
if (j < 29) {
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;
}
}
choose.close();
Scanner choose=新扫描仪(System.in);
字符串选择=null;
int j=0;
而(!“结束”。等于(选择)){
System.out.println(“\n键入1、2或3,然后按enter键查看相应贷款的每月付款信息。要结束程序,请键入\“end\”);
choice=choose.nextLine();
如果(“1”。等于(选择)){
//计算代码
if(j<6){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=choose.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
choice=null;
}
如果(“2”。等于(选择)){
如果(j<14){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=choose.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
choice=null;
}
如果(“3”。等于(选择)){
如果(j<29){
System.out.println(“按enter键获取”+(j+2)年的抵押信息);
choice=choose.nextLine();
}否则{
System.out.println(“恭喜你,你的抵押贷款已经还清了。”);
}
choice=null;
}
}
选择。关闭();
导入java.util.Scanner
班长
{
}while循环中的代码是重复的。考虑创建一个新的可重用方法。问问自己,这些代码中有多少与您的问题相关,尤其是那些
println()
调用?请仔细阅读并严格编辑您发布的代码,不要随意复制粘贴大量代码。通过这样做,您将经常回答您自己的问题,因为再现问题所需的代码会减少,所以您将重点放在导致问题的代码上
Scanner choose = new Scanner(System.in);
String choice= null;
int j = 0;
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 = choose.nextLine();
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)) {
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)) {
if (j < 29) {
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;
}
}
choose.close();
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.1:");
int a=sc.nextInt();
System.out.println("Enter no.2:");
int b=sc.nextInt();
boolean exit=false;
do
{
System.out.println("1.addition");
System.out.println("2.subtraction");
System.out.println("3.multiplication");
System.out.println("4.division");
System.out.println("5.exit");
System.out.println("choose one!");
Scanner sd=new Scanner(System.in);
System.out.println("enter your choice");
int num=sd.nextInt();
switch(num)
{
case 1:
int add=a+b;
System.out.println("addition="+add);
System.out.println("\n");
break;
case 2:
int sub=a-b;
System.out.println("subtraction="+sub);
System.out.println("\n");
break;
case 3:
int mul=a*b;
System.out.println("multilpication="+mul);
System.out.println("\n");
break;
case 4:
int div=a/b;
System.out.println("division="+div);
System.out.println("\n");
break;
case 5:
exit=true;
break;
}
}while(!exit);
}