Ejercicio
Contraseña
Objetivo
Escribe un programa en java para pedir al usuario su login y su contraseña (ambos deben ser números enteros) y repítelo tantas veces como sea necesario, hasta que el login introducido sea "12" y la contraseña sea "1234".
Código de Ejemplo
// 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!");
}
}