Reputation: 11
I wrote this code using NetBeans 7.3. It is an elustration for a simple ATM program. My problem is I cannot view the menu options more than twice. In the second time it is repeated I cannot inter the switch. What can I do to fix this problem??
This is my code:
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();
}
}
}}
Upvotes: 1
Views: 179
Reputation: 76567
Your code is confused.
You've got 2 routines named main
.
The main with the following signature is your proper main function, which gets called when starting the application:
public static void main(String[] args) {
So this gets called first.
Inside this function you call the other main()
let's call it main2 to avoid confusion.
In main2 you call exit
which terminates the program.
So it's exactly right that your program only runs twice.
You can fix the problem by making your program a sane one.
The structure should go like this:
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);
}
Then you create functions for displayMenu()
, getUserInput()
, getCreditCardNumber()
, displayErrorMessage()
.
Note that all the *get*Functions must return whatever it is that they're supposed to get.
Upvotes: 1