I have to write a program which checks that if the password entered by the user is 'bolt', it will display 'The password is valid' otherwise it will display 'The password is invalid'.
The program is working only for the if part but not for the else that is when I input bolt it displays the correct message but when I enter something other than bolt, it does not display 'The password is invalid'. I have been told to use char.At
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your 4-character password:");
String password = input.next();
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) == 'B' || password.charAt(i) == 'b')
if (password.charAt(i + 1) == 'O'
|| password.charAt(i + 1) == 'o')
if (password.charAt(i + 2) == 'L'
|| password.charAt(i + 2) == 'l')
if (password.charAt(i + 3) == 'T'
|| password.charAt(i + 3) == 't') {
System.out.println("The password is valid");
}
else {
System.out.println("The password is invalid!");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire