java - While循环和switch

标签 java loops menu while-loop switch-statement

我使用 NetBeans 7.3 编写了此代码。这是一个简单的 ATM 程序的说明。我的问题是我无法查看菜单选项两次以上。第二次重复时我无法插入开关。我该怎么做才能解决这个问题?

这是我的代码:

public static void main() {

    System.out.println("            ****************************************************" );
    System.out.println("            * Can you please choose which one of the following *" );
    System.out.println("            *   services you want the program to perform by    *" );
    System.out.println("            *    typing down the number of the option below:   *" );
    System.out.println("            *                                                  *" );
    System.out.println("            *       1. Test credit card number.                *" );
    System.out.println("            *       2. Exit.                                   *" );
    System.out.println("            ****************************************************" );

    int choice;
    System.out.print("your choice is: ");
    choice = console.nextInt();
    //while (choice == 1 || choice != 2)
    if (choice == 2) {
        System.out.println("                 *** Please visit us again. ***");
        System.exit(0);        
    } 
}

public static void main(String[] args) {

    int choice; 
    System.out.println("            *****************************************************" );
    System.out.println("            *   Welcome to the credit card number test program  *" );
    System.out.println("            *                                                   *" );
    System.out.println("            *    First we would like to thank you for choosing  *" );
    System.out.println("            *  our program and we hope you will find it useful  *" );
    System.out.println("            *                                                   *" );
    System.out.println("            *  We guarantee you that you will receive the best  *" );
    System.out.println("            *               services in the world.              *" );
    System.out.println("            *****************************************************" );


    System.out.print("your choice is: ");
    choice = console.nextInt();

    switch (choice) {   
        case 1:
        int[][] credit_number = new int [3][16];
        int row;
        int col;
        int sum;
        String statue;

        System.out.println("Please enter 16 number for a credit card: " ); 

        row = 0;
        {    
            for (col = 0; col < credit_number[row].length; col++)
            credit_number[row][col] = console.nextInt();
        }     

      while (choice == 1 || choice != 2)
          main();
      System.out.println();
      break;      

  case 2:
      System.out.println("                 *** Please visit us again. ***");
      System.exit(0);

      default: {
          System.out.println("Warning: Please make sure to choose an available option from the menu.");
          main();
      }
   }
}}

最佳答案

你的代码很困惑。 您有 2 个名为 main 的例程。

具有以下签名的 main 是您正确的主函数,在启动应用程序时会调用该函数:

public static void main(String[] args) {

所以这首先被调用。
在这个函数中,您调用另一个 main() 我们将其称为 main2 以避免混淆。

在 main2 中,您调用 exit 来终止程序。

所以你的程序只运行两次是完全正确的。

您可以通过使您的程序变得正常来解决该问题。

  1. 你永远不应该重复自己。
  2. 使用有意义的名称。
  3. 让函数有返回值。
  4. 请记住,函数的本地变量在该函数之外是不可见的 (google +java +scope +variable)

结构应该是这样的:

public static void main(String[] args) {

  boolean areWeDoneYet = false;
  string ccNumber;

  while !(areWeDoneYet) {
    displayMenu();
    int choice = getUserInput();
    switch (choice) {
      case 1: 
        ccNumber = getCreditCardNumber();
        processCreditCardNumber(ccNumber);  
        break;
      case 2:  
        areWeDoneYet = true;
        break; 
      default: 
        displayErrorMessage();
        //waitForUserToConfessHisSins();
        //fineUser();
        //questionMark();
        //dots();
        //profit();
    } //switch 
  } //while
  exit(0);
}

然后为 displayMenu()getUserInput()getCreditCardNumber()displayErrorMessage() 创建函数。
请注意,所有 *get* 函数都必须返回它们应该获取的任何内容。

关于java - While循环和switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19263493/

相关文章:

java - Hibernate 代理序列化并在客户端接收

java - 如何在 JAVA (Netbeans IDE) 上进行自定义绑定(bind)

java - 如何通过单击 android 中的按钮完成一个循环后继续 R.raw.audio 声音

php - 如何检查是否可以从字母列表中创建单词?

windows - 在 Linux 中自动运行可移动设备

java - AJAX 请求中可能存在 SQL 注入(inject)吗?

java - 试图产生不重叠的气泡。为什么这不起作用?

python - 编写一个 for 循环来打印 1 个嵌套字典的键

html - 粘性菜单中的元素在滚动时被按下

带有菜单功能的 Python 主函数循环不起作用?