Java String Switch语句未转到正确的开关
我目前面临着我的switch语句的问题。我已经打印出了switch变量,它似乎是正确的,但是将print语句打印到所需的开关中是不起作用的。switch语句位于从底部开始的主~20行中 测试存根为:Java String Switch语句未转到正确的开关,java,variables,switch-statement,Java,Variables,Switch Statement,我目前面临着我的switch语句的问题。我已经打印出了switch变量,它似乎是正确的,但是将print语句打印到所需的开关中是不起作用的。switch语句位于从底部开始的主~20行中 测试存根为: "Person#Joe Gaucho#28000 Marguerite Parkway#949-582-4800#gaucho@sb.edu;" "Faculty#1234153524#1341244#1234150000#Adjunct;Employee#bgs210#20000#12345500
"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
我的输出是:
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);
}
}
您需要在每个循环结束时将myClass的值重置为一些默认值(通常为null)。您获得此重复输出是因为: 它查找类型为Person的输入 它使用新的Person对象更新myClass。 循环从顶部开始,找到一个新的教员 myClass不会被覆盖,因此Person会保留并再次输出。
我已经在一家公司复制了这种行为 我也假设您的测试存根是您正在解析的一个大字符串,包括?没有引起您的问题,但是为什么所有的静态类?是的,它都是一个大字符串。它不包含引号,但我在没有引号的情况下很难将其放入堆栈溢出,因为分号分隔符。@Mickey如果没有static,它在main中无法工作。我认为这是因为对象类的toString重写。通常,在main中尽可能少地执行。您可以在Exercise10\u 2M中将大部分代码主要移动到构造函数中