我使用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();
}
}
}}
我是唯一一个发现这个使用了两个重载'main'函数的丑陋? – Floris
我正要评论@Floris。我不知道有足够的java规则来确定第二个主函数是如何或何时从第一个函数调用的?这肯定是你问题的根源。不是递归调用callibg main,而是将整个构造放入一个while循环中 - 看起来像一次基于注释行的方向。并坚持一个主要的,并命名其他方法。根据需要明确地调用它们。 –
@弗洛里斯,是的,我总是把所有的功能都叫做主要的。当签名相同时,我将它们称为main1,main2等。它适用于我。 – Johan