2013-10-09 84 views
1

我使用NetBeans 7.3编写了此代码。这是一个简单的ATM程序的插图。我的问题是我无法查看菜单选项超过两次。在第二次重复时,我不能在交换机上进行交换。我能做些什么来解决这个问题?While循环和开关

这是我的代码:

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(); 
     } 
    } 
}} 
+4

我是唯一一个发现这个使用了两个重载'main'函数的丑陋? – Floris

+0

我正要评论@Floris。我不知道有足够的java规则来确定第二个主函数是如何或何时从第一个函数调用的?这肯定是你问题的根源。不是递归调用callibg main,而是将整个构造放入一个while循环中 - 看起来像一次基于注释行的方向。并坚持一个主要的,并命名其他方法。根据需要明确地调用它们。 –

+0

@弗洛里斯,是的,我总是把所有的功能都叫做主要的。当签名相同时,我将它们称为main1,main2等。它适用于我。 – Johan

回答

1

您的代码混淆。 你有两个例程main

主要具有以下签名是你的正确的主要功能,启动应用程序时都会调用:

public static void main(String[] args) { 

因此,这被称为第一。
在这个函数里面你可以调用其他的main()我们称之为main2来避免混淆。

在MAIN2你叫exit终止程序。

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

您可以通过你的程序一个健全的一个解决这个问题。

  1. 你不应该重复自己。
  2. 使用有意义的名称。
  3. 有函数返回值。
  4. 记住,局部变量的函数是函数外部不可见的(谷歌+的java +范围+变量)

结构应该是这样的:

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()
注意,所有的* 得到 *函数必须返回什么,那就是他们应该得到的。

+0

这段代码结构看起来更可读。我相信它会有所帮助。 – Floris