Menu - Ifs and Loops

You can put ifs and loop together to do provide feedboack for users.

Example: Password Program

Enter the password: wekjadfa
Sorry please try again.
Enter the password: kjlaer
Sorry please try again.
Enter the password: 123abcABC
Correct, please continue
public class password
{
   public static void main (String args [])
   {
   new password ();
   }
   public password ()
   {
   String secret = "123abcABC";
   String guess = "";
   
   //while they haven't got the password yet
   while (!guess.equals (secret))
   {
       guess = IBIO.inputString ("Enter the password: ");
       if (!guess.equals (secret)) 
   		System.out.println ("Sorry please try again.");
   }
   //if they are out of the loop, they got the password
   System.out.println ("Correct, please continue");
   }
}

Example: Password that stops after 5 tries

Enter the password: eare
Sorry please try again.
Enter the password: aglkjfa
Sorry please try again.
Enter the password: aflgkafd
Sorry please try again.
Enter the password: afgk
Sorry please try again.
Enter the password: li3-94
Sorry please try again.
Too many unsuccessful attempts. Account locked
public class password2
{
   public static void main (String args [])
   {
   	new password2 ();
   }
 	public password2 ()
   {
   	String secret = "123abcABC";
   	String guess = "";
   	int count = 0; //to count their guesses.
 		//while they haven't got it and they haven't guessed too many times
   	while (!guess.equals (secret) && count < 5)
   	{
   		guess = IBIO.inputString ("Enter the password: ");
   		if (!guess.equals (secret))
   		{
   			System.out.println ("Sorry please try again.");
   			count++;
  			 }//if for incorrect guess
   	}//while they don't get the password right
 		if (count >= 5) //if they were unsuccessful 5 times
   		System.out.println ("Too many unsuccessful attempts. Account locked");
   	else //else they have logged on
   		System.out.println ("Correct, please continue");
   }
}
 

Example: A Menu that Prints Greetings

W E L C O M E   T O   T H E   M E N U        
*********************************
* M E N U *
* (1) Print Hello *
* (2) Print Hi *
* (3) Print Top o' the morning *
* (4) Print Bonjour *
* (5) Quit *
*********************************
Your choice >> 3
T O P 
O' 
T H E 
M O R N I N G 
*********************************
* M E N U *
* (1) Print Hello *
* (2) Print Hi *
* (3) Print Top o' the morning *
* (4) Print Bonjour *
* (5) Quit *
*********************************
Your choice >> 4
 B O N J O U R 
*********************************
* M E N U *
* (1) Print Hello *
* (2) Print Hi *
* (3) Print Top o' the morning *
* (4) Print Bonjour *
* (5) Quit *
*********************************
Your choice >> 5
 G O O D B Y E 
public class menuExample
   {
   public static void main (String args[])
   {
   new menuExample ();
   }
   public menuExample ()
   { //insert welcome message here, only displayed once
   System.out.println ("W E L C O M E T O T H E M E N U");
   System.out.println ("");
 //print menu for the first time before the loop
   System.out.println ("*********************************");
   System.out.println ("* M E N U *");
   System.out.println ("* (1) Print Hello *");
   System.out.println ("* (2) Print Hi *");
   System.out.println ("* (3) Print Top o' the morning *");
   System.out.println ("* (4) Print Bonjour *");
   System.out.println ("* (5) Quit *");
   System.out.println ("*********************************");
   System.out.println ("");
   char choice = IBIO.inputChar ("Your choice >> ");
   System.out.println ("");
 //start the loop, continue until the user chooses 5 - quit
   while (choice != '5')
   {
   	if (choice == '1') //1. Print Hello
   	{
   		System.out.println (" H E L L O ");
   	}
   	else if (choice == '2') //2. Print Hi
   	{
  			System.out.println (" H I ");
   	}
   	else if (choice == '3') //3. Print Top o' the morning
   	{
   		System.out.println (" T O P ");
   		System.out.println (" O' ");
   		System.out.println (" T H E ");
   		System.out.println (" M O R N I N G ");
   	}
   	else if (choice == '4') //4. Print Bonjour
   	{
   		System.out.println (" B O N J O U R ");
   	}
   	else //user did not enter one of the options
   	{
   		System.out.println (" Please enter one of the options provided.");
   	}
 //print the menu again so the user can see it and choose again
   System.out.println ("");
   System.out.println ("*********************************");
   System.out.println ("* M E N U *");
   System.out.println ("* (1) Print Hello *");
   System.out.println ("* (2) Print Hi *");
   System.out.println ("* (3) Print Top o' the morning *");
   System.out.println ("* (4) Print Bonjour *");
   System.out.println ("* (5) Quit *");
   System.out.println ("*********************************");
   System.out.println ("");
   choice = IBIO.inputChar ("Your choice >> ");
   System.out.println ("");
 }
 //after the loop is finished, program is over
   System.out.println (" G O O D B Y E ");
   }
}