Java字符串开关语句不能正确切换

时间:2014-10-20 15:45:58

标签: java variables switch-statement

我目前正面临着我的switch语句问题。我打印出了switch变量,看起来是正确的,但是打印到所需开关的语句不起作用。 switch语句位于从底部开始的~20行中。

测试存根是:

"Person#Joe Gaucho#28000 Marguerite Parkway#949-582-4800#gaucho@sb.edu;"
"Faculty#1234153524#1341244#1234150000#Adjunct;Employee#bgs210#20000#123455000;"
"Person#Mickey Mouse#Disneyland#800 1Disney#mmouse@disney.com;"
"Exit#"

预期输出为:

Person      name: Joe Gaucho        address: 28000 Marguerite Parkway      phoneNumber: 949-582-4800        emailAddress: gaucho@sb.edu
Faculty OfficeHours: Wed Jan 14 22:49:13 PST 1970, Wed Dec 31 16:22:21 PST 1969, Wed Jan 14 22:49:10 PST 1970   rank: Adjunct
Employee        office: bgs210  salary: 20000.0 dateHired: Fri Jan 02 02:17:35 PST 1970
Person  name: Mickey Mouse      address: Disneyland     phoneNumber: 800 1DisneyemailAddress: mmouse@disney.com

我的输出是:

Person  name: Joe Gaucho    address: 28000 Marguerite Parkway   phoneNumber: 949-582-4800   emailAddress: gaucho@sb.edu
Person  name: Joe Gaucho    address: 28000 Marguerite Parkway   phoneNumber: 949-582-4800   emailAddress: gaucho@sb.edu
Employee    noffice: bgs210 salary: 20000.0 dateHired: 123455000
Employee    noffice: bgs210 salary: 20000.0 dateHired: 123455000

这是我的代码:

import java.util.Scanner;
public class Exercise10_2M {
    static class Person{
        String name;
        String address;
        String phoneNumber;
        String emailAddress;

        Person(){}
        Person(String name, String address, String phoneNumber, String email){
            this.name = name;
            this.address = address;
            this.phoneNumber = phoneNumber;
            this.emailAddress = email;
        }

        @Override 
        public String toString(){
            return "Person\tname: " + name
                    + "\taddress: " + address
                    + "\tphoneNumber: " + phoneNumber
                    + "\temailAddress: " + emailAddress + "\n";
        }
    }
    static class Student extends Person{
        String classStatus; // freshmen, sophomore, sophomore+, junior, or senior

        Student(){}
        Student(/*String str1, String str2, String str3, String str4,*/ String str5){
            //super(str1, str2, str3, str4);
            classStatus = str5;
        }
        @Override 
        public String toString(){
            return "Student\tclassStatus: "+ classStatus + "\n" /*+ super.toString()*/;
        }
    }
    static class Employee extends Person{
        String office;
        double salary;
        long dateHired;
        Employee(){}
        Employee(String str, double doub, long long1){
            office = str;
            salary = doub;
            dateHired = long1;
        }
        @Override 
        public String toString(){
            return "Employee\tnoffice: " + office
                    + "\tsalary: " + salary
                    + "\tdateHired: " + dateHired + "\n";
        }
    }
    static class Faculty extends Employee{
        long[] officeHours;
        String rank;        // either adjunct, assistantProfessor, or Professor

        Faculty(){}
        // !!!! FIX

        /*Faculty(long long1, String str){
            officeHours = long1;
            rank = str;
        }*/

        @Override
        public String toString(){
            return "Faculty\tofficeHours: " //+ officeHours[0]
                    + "\trank: " + rank + "\n";
        }
    }
    static class Staff extends Employee{
        String title;

        Staff(){}
        Staff(String str){
            title = str;
        }
        @Override
        public String toString(){
            return "Staff\ttitle: " + title + "\n";
        }
    }
    public static void main(String[] args){
        Scanner firstScan = new Scanner(System.in); // Scanner
        String firstInput;                          // Parses from scanner
        String[] myInput;                           // Parses from firstInput
        Person myClass = new Person();              // Class instance
        String typeOfPerson = new String();         // Holds value for which class
        boolean LCV = true;

        firstScan.useDelimiter(";");

        do{
            firstInput = firstScan.next();
            myInput = firstInput.split("#");
            typeOfPerson = myInput[0];

            switch(typeOfPerson){
                case "Person":   myClass = new Person(myInput[1], myInput[2],
                                                      myInput[3], myInput[4]);
                                           break;
                case "Student":  myClass = new Student(/*scan.next(), scan.next(),
                                                       scan.next(), scan.next(),*/
                                                       myInput[1]);
                                           break;
                case "Employee": myClass = new Employee(myInput[1],            
Double.parseDouble(myInput[2]), Long.parseLong(myInput[3])); 
                                           break;
                case "Faculty":  System.out.println("check");
                    //myClass = new Faculty();
                                           break;
                case "Staff":    myClass = new Staff(myInput[1]);
                                           break;
                case "Exit": LCV = false;
                                   break;
            }
            if(LCV == true){
                System.out.print(myClass.toString());
            } 
        }while(firstScan.hasNext() && LCV == true);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在每个循环结束时将myClass的值重置为某个默认值(通常为null)。你得到这个重复的输出是因为:

  • 找到类型Person
  • 的输入
  • 使用新的myClass对象更新Person
  • 循环从顶部开始,找到新的Faculty
  • myClass未被覆盖,ergo Person仍然存在并再次输出。

我在this IDEOne中复制了这种行为。