dimanche 22 juillet 2018

Java- Comparing names to a list of names using if/else and switch statements

I am a beginner coder who is trying to figure out how to use nested if/else and switch statements. I decided to challenge myself to write 2 programs (one with if/else and one with switch) that will compare 1 person's name to at least 15 other people's. If that 1 name matches with any of the 15 other names (besides that person's own name), it will print something. For example, the 15 names will be the names of everyone on a football team. I want to see if someone on the team has someone that shares their name. For the if/else program, I currently have this:

package practice;

    public class ClassmatesIfElse {

        public static void main(String[] args) {
            String name = "person";
            if(name.equals("person1")|| name.equals("person2") || name.equals("person3") || name.equals("person4") || name.equals("person5") || name.equals("person6") || name.equals("person7") || name.equals("person8") || name.equals("person10"))
                System.out.println("Hey! You have a name doppleganger in the class!");
            else System.out.println("Your name is unique in this class!");
        }

    }

But obviously, it is very cumbersome and it doesn't work unless you remove the person you want to test from the list, which kind of defeats the purpose of the program. And I do not know where to start for the switch statement one. What is the best way to go about this? Thank you.

Aucun commentaire:

Enregistrer un commentaire