Password V2 Learn programming Java

Lesson:

Flow Control


Exercise:

Password V2 51


Objetive:

Write a java program to ask the user for his/her login and his/her password (both must be integer numbers), until the entered login is "12" and the password is "1234". The user will have 3 attempts maximum.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int user, pass;
		int counter = 0;

		do
		{
			System.out.print("Enter a user:  ");
			user = Integer.parseInt(new Scanner(System.in).nextLine());

			System.out.print("Enter a password:  ");
			pass = Integer.parseInt(new Scanner(System.in).nextLine());

			if ((user != 12) || (pass != 1234))
			{
				System.out.println("Login Error");
				counter++;
			}

		} while (((user != 12) || (pass != 1234)) && (counter != 3));

		if ((user != 12) || (pass != 1234))
		{
			System.out.println("Logged out!");
		}
		else
		{
			System.out.println("Login successful");
		}
	}
}