Я написал этот код, используя NetBeans 7.3. Это является иллюстрацией для простой программы ОрВД. Моя проблема в том, что я не могу просматривать параметры меню более двух раз. Во второй раз это повторяется, я не могу переключаться между ними. Что я могу сделать, чтобы исправить эту проблему?While loop & switch
Это мой код:
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();
}
}
}}
Я единственный, кто находит это использование двух перегруженных 'главных' функций уродливыми? – Floris
Я собирался прокомментировать то же, что @Floris. Я не знаю достаточно java-правил, чтобы определить, как и когда вторая основная функция даже вызывается из первого? Это, безусловно, должно быть источником ваших проблем. Вместо рекурсивно callibg main, поместите всю конструкцию в цикл while - похоже, что вы были направлены таким образом, как только на основе прокомментированных строк. И придерживайтесь одной основной, и называйте другие методы чем-то другим. Вызывайте их явно по мере необходимости. –
@Floris, да, я всегда называю все мои функции основными. И когда подписи одинаковы, я называю их main1, main2 и т. Д. Это работает для меня. – Johan