While Loops #60 (Another if statement, but repeating!)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name:Enter PIN
/// File Name: EnterPIN.java
/// Date Finished: 12/3/2015

import java.util.Scanner;
//How is a while loop similar to an if statement? Well it follows the same concept of if the varible is correct.
//How is a while loop different from an if statement? Unlike the if statement, the while statemnet will loop until givin the right answer.
//Inside the while loop, why isn't there an int in front of the line entry = keyboard.nextInt()? Because the while statement looks above to find established variables. 
//Delete the line entry = keyboard.nextInt(); from inside the while loop. What happens? The compliling fails Why? because it can't navigate back to the original entry variable.

public class EnterPIN
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);
		int pin = 97458;

		System.out.println("WELCOME TO THE BANK OF AMIR.");
		System.out.print("ENTER YOUR PIN: ");
		int entry = keyboard.nextInt();

		while ( entry != pin )
		{
			System.out.println("\nINCORRECT PIN. TRY AGAIN.");
			System.out.print("ENTER YOUR PIN: ");
			 entry = keyboard.nextInt();
		}

		System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
	}
}
    

Picture(s) of the output

Assignment 7

Back to Homepage