首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >While循环和开关

While循环和开关
EN

Stack Overflow用户
提问于 2013-10-09 12:58:08
回答 1查看 166关注 0票数 1

我使用NetBeans 7.3编写了这段代码。这是一个简单的自动柜员机程序的洗脱。我的问题是我不能查看两次以上的菜单选项。在第二次重复时,我无法插入开关。我能做些什么来解决这个问题?

这是我的代码:

代码语言:javascript
复制
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();
      }
   }
}}
EN

回答 1

Stack Overflow用户

发布于 2013-10-09 13:23:22

你的代码很混乱。您有两个名为main的例程。

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

代码语言:javascript
复制
public static void main(String[] args) {

所以这个函数首先被调用。

在这个函数中,我们调用另一个main(),让我们称它为main2,以避免混淆。

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

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

你可以通过让你的程序正常工作来解决这个问题。

  1. 你永远不应该重复自己。
  2. 使用有意义的名称。
  3. 具有函数返回值。
  4. 记住,一个函数的局部变量在该函数外部是不可见的(google +java +

+)

结构应该是这样的:

代码语言:javascript
复制
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 *函数必须返回它们应该得到的任何东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19263493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档