简体   繁体   中英

Both branches are being executed in my if-else statement

When I ask for a faculty input, it will execute correctly until the output is printed, it will then print ask for the 800 number (which is only to be asked for students). I can't figure out why it always asks for the student specific questions after you input faculty.

    public class UniversityIDApp {
public static void main(String[] args){
    // display a welcome message
    System.out.println("Welcome to the University ID application");
    System.out.println();

    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while(choice.equalsIgnoreCase("y"))
    {
        // get user entries
        String userType = Validator.getString(sc,
            "Create student or faculty? (s/f):   ", "s", "f");
        String firstName = Validator.getString(sc,
            "Enter first name:   ");
        String lastName = Validator.getString(sc,
            "Enter last name:   ");
        String email = Validator.getString(sc,
            "Enter e-mail:  ");


    if (userType.equalsIgnoreCase("f"))
    {

    String department = Validator.getString(sc,
            "Enter department:   ");

       // create faculty object
       Faculty f = new Faculty() {};
       f.setFirstName(firstName);
       f.setLastName(lastName);
       f.setEmail(email);
       f.setDepartment(department);
       System.out.print("Southern Illinois University Faculty ");
       System.out.print("\nName:       " + f.getFirstName() + " " + f.getLastName() + "\n" 
       +  "Email:      "  + f.getEmail() + "\n" + "Department: " + f.getDepartment() + "\n" );
       System.out.println();
    }
    else if(userType.equalsIgnoreCase("s"));
    {

    Student s = new Student() {};
    String student800Number = Validator.getString(sc,
            "Enter your 800 number:   ");

       s.setFirstName(firstName);
       s.setLastName(lastName);
       s.setEmail(email);
       s.setStudent800Number(student800Number);
       System.out.print("Southern Illinois University Student");
       System.out.print("\nName:        " + s.getFirstName() + " " + s.getLastName() + "\n" 
  +   "Email:       "  + s.getEmail() + "\n" + "Student 800 Number:  " + s.getStudent800Number() + "\n" );
       System.out.println();

        }
        System.out.print("Continue? (y/n): ");
        choice = sc.next();
        System.out.println();
    }
}

your else if clause has a semicolon ; making it do nothing.

...
}
   else if(userType.equalsIgnoreCase("s")); //<-- here
{
...

Which is the same as this:

        if ( true ) {
            System.out.println("true");
        } else if ( false ) {
            ;
        }

        {
            System.out.println("false? what's going on?");
        }

This could've been easier to spot if you put the opening brace in the same line (which is Java's convention )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM