public class array {
public static void main(String[] args) {
String names[] = {"John", "Mary", "Peter"};
Scanner myScanner = new Scanner(System.in);
System.out.print("Enter name: ");
String nameEntered = myScanner.nextLine();
for (int i = 0; i <= names.length; i++) {
if (nameEntered.equals(names[i])) {
System.out.println(names[i] + " is a common name.");
} else {
System.out.println(nameEntered + " is not a common name.");
}
}
}
}
How do I have the else statement only print out once? If I do it like this the output it nameEntered + " it is not a common name." three times. It can't be break; right since it'll print some weird stuff when the if statement is met. If I use continue it'll check all three and the output will be like "Mary is not a common name" "Mary is a common name" "Mary is not a common name" if I input Mary
Aucun commentaire:
Enregistrer un commentaire