Exercise
Password
Objetive
Write a java program to prompt the user to enter their login and password (both must be integer numbers) and repeat the prompt as many times as necessary until the entered login is "12" and the password is "1234".
Example Code
// Import the Scanner class to read input from the user
import java.util.Scanner;
public class Main {
// The main method is the entry point of the program
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Initialize login and password variables to store user inputs
int login, password;
// Use a do-while loop to repeatedly ask for login and password until they are correct
do {
// Prompt the user to enter their login
System.out.print("Enter your login: ");
login = scanner.nextInt(); // Read the login input from the user
// Prompt the user to enter their password
System.out.print("Enter your password: ");
password = scanner.nextInt(); // Read the password input from the user
// Check if the login or password are incorrect
if (login != 12 || password != 1234) {
// If incorrect, display an error message and prompt again
System.out.println("Incorrect login or password. Please try again.");
}
} while (login != 12 || password != 1234); // Continue the loop until the correct login and password are entered
// Once the correct login and password are entered, display a success message
System.out.println("Login successful!");
}
}