Java - 菜单选择的 While 循环(基于控制台的程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13576734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:16:48  来源:igfitidea点击:

Java - While loop for menu selection (console based program)

java

提问by Dave

UpdateThe first time the user makes a choice such as "1" the menu is displayed again. The next time a selection is made the payment information begins to cycle. After the cycling is complete and the menu is displayed again, it works as it should. Also, the first two years are output instead of just the first when a selection begins to cycle, but then outputs one year at a time as intended.

更新用户第一次做出选择(例如“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();
}

}

}

回答by Yogendra Singh

I can see three issues upfront:

我可以预先看到三个问题:

  1. You don't need two scanners for System.instream. Remove this statement Scanner next = new Scanner(System.in);and use the chooseinstance.

  2. If you close your input scanner as next.close();, it closes your input stream System.inas well and you may not be able read the stream again. Make sure you close the stream only when you are completely done with your program.

  3. Use equalsmethod to compare the condition in whileas while(!"end".equals(choice)). Put the literal "end"as first argument will take care of the null value of choice.

  1. 您不需要两个扫描仪进行System.in流媒体播放。删除此语句Scanner next = new Scanner(System.in);并使用choose实例。

  2. 如果您将输入扫描器next.close();关闭为,它System.in也会关闭您的输入流,您可能无法再次读取该流。确保仅在您完全完成程序后才关闭流。

  3. 使用equals方法比较whileas 中的条件while(!"end".equals(choice))。将文字"end"作为第一个参数将处理choice.

EDIT:

编辑:

Your modified code at high level:

您修改后的高级代码:

    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();

回答by awolfe91

One bug is that String equality isn't the same as ==. You should use .equals():

一个错误是字符串相等性与 == 不同。你应该使用 .equals():

while (!choice.equals("end")) {

Hope that helps!

希望有帮助!

回答by Soumya

You're setting choiceto null, so choice != "end"is always true. Move the next.close(); choice = nullto outsidethe while loop. Also, what weolfe91 said.

您设置choicenull,所以choice != "end"总是如此。将next.close(); choice = nullto 移到while 循环之外。还有,weolfe91 说的。

回答by sandeep shukla

import java.util.Scanner;

导入 java.util.Scanner;

class Main

主类

{

{

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);
}

}

}