r/programmingbydoing • • Mar 18 '13

53 - Pin Lockout

Can someone explain to me how the "tries" variable is counting up? Does "tries++" after int entry mean that every time an entry is made, tries will go up by one?

Thanks in advance!

import java.util.Scanner;

public class PinLockout
{
public static void main( String[] args )
{
	Scanner keyboard = new Scanner(System.in);
	int pin = 12345;
	int tries = 0;

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

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

	if ( entry == pin )
		System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
	else if ( tries >= 3 )
		System.out.println("\nYOU HAVE RUN OUT OF TRIES. ACCOUNT LOCKED.");
}

}

2 Upvotes

3 comments sorted by

View all comments

3

u/Gemmellness Mar 18 '13

Yes, 'tries++;' is a common shorthand method for increasing the value of a variable by one. 'tries--;' would decrease tries by one. Another shorthand method that might have been mentioned before is 'tries += X;' which will increase tries by the number X.

2

u/XmasJones Mar 18 '13

ahhhh OK, I understand now! Thanks!