You'll assign the password to a variable, And the user will be prompted to enter a password. Their attempt is assigned to another variable and you'll compare them. If they don't match "INVALID" is displayed and they will continue to be prompted for a password until the correct one is entered.
package arrays;
import java.util.Scanner;
public class searchingalgorithms {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int password= 9320;
int user = 0;
System.out.print("Enter a password: :");
user = sc.nextInt();
if(user==password){
System.out.println("Valid");
}
if(user!=password){
System.out.println("INVALID");
user = sc.nextInt();
}
}
}
Hi so, my program works the first time if it is valid but, it just keeps on going on and on. Also, What I want my program to do is repeat the program if it is invalid. How do I do this?
Aucun commentaire:
Enregistrer un commentaire