使用带有阵列的开关盒

时间:2018-04-26 01:16:46

标签: java arrays switch-statement

是否可以在此代码中使用交换机语句中的数组? 因为我尝试了几次但我不能,还有另一种方法来声明BloodType然后为它分配多个String值而没有这个数组?例如(enum或ArrayList?)我可以在这种情况下使用它们吗?

private String []BloodType  = new String[8];
public String[] getBloodType() {
        return BloodType;
    }

    public void setBloodType(String[] BloodType) {
        this.BloodType = BloodType;
    }
        private void BloodType()
        {
            BloodType[0]="A+";
            BloodType[1]="A-";
            BloodType[2]="B+";
            BloodType[3]="B-";
            BloodType[4]="O+";
            BloodType[5]="O-";
            BloodType[6]="AB+";
            BloodType[7]="AB-";
        }
public void BloodInfomation () // use BloodType here as Array
    {

            switch (BloodType) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }

3 个答案:

答案 0 :(得分:2)

你是对的。还有另一种方法来实现你的BloodType东西。你提到枚举,它是一个更好的实现对象,如血型(具有有限数量的实例变化)。

enum BloodType {
    A_MINUS("A+", 1), 
    B_PLUS("B+", 2), 
    B_MINUS("B-", 3), 
    O_PLUS("AO", 4), 
    O_MINUS("O-", 5), 
    AB_PLUS("AB+", 6), 
    AB_MINUS("AB-", 7);

    BloodType(String value, int code) {
        this.value = value;
        this.code = code;
    }

    String value;
    int code;

}

void getBloodInformation(BloodType type) {
    switch (type) {
    case A_MINUS:
        // do something ...
        break;
    case O_MINUS:
        // do other things
        break;
    default:
        // default behavior if you didn't define all cases above
    }
}

使用带开关的枚举更好。这样您就不会错过一个案例,并避免因错误的字符串拼写而导致的潜在错误。

答案 1 :(得分:0)

开启阵列无效。相反,查看数组中的项目并打开每个项目。要做到这一点,你需要将开关包装在一个循环中,以迭代BloodInformation方法中的血型。

public void BloodInfomation () // use BloodType here as Array
{
          for(String type : BloodType) {
            switch (type) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }
     }

答案 2 :(得分:0)

该数组可以很好地存储血型,但您的比较不起作用。在您的交换机中,您将字符串的字符串与String []进行比较。我想你想要的是两个独立的变量,一个是血型阵列,另一个是你所指的对象的特定血型。

public class person{
//Defining the array of possible blood types which
private String[] BloodTypeArray = new String[8];
private void BuildBloodTypeArray(){
    BloodTypeArray[0]="A+";
    BloodTypeArray[1]="A-";
    BloodTypeArray[2]="B+";
    BloodTypeArray[3]="B-";
    BloodTypeArray[4]="O+";
    BloodTypeArray[5]="O-";
    BloodTypeArray[6]="AB+";
    BloodTypeArray[7]="AB-";
}
//Defining the specific blood type of this instance
private String BloodType;
public String getBloodType(){
    return BloodType;
}
public void setBloodType(String BloodType){
    this.BloodType = BloodType;
}
//The switch needs something to compare the case to so it takes an input string
public void BloodInformation(){
    int BloodCode;
    switch(this.BloodType){
        case "A+":
        BloodCode=1;
        System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "A-":
        BloodCode=2;
        System.out.println("Blood Type A- & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "B+":
        BloodCode=3;
        System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "B-":
        BloodCode=4;
        System.out.println("Blood Type B- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O+":
        BloodCode=5;
        System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O-":
        BloodCode=6;
        System.out.println("Blood Type O- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB+":
        BloodCode=7;
        System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB-":
        BloodCode=8;
        System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    default:
        System.out.println("There is no code for the type you write !!");
        break;
    }
}
public static void main(String[] args){
    person p = new person();
    p.BuildBloodTypeArray();
    p.setBloodType("AB-");
    System.out.println(p.getBloodType());
    p.BloodInformation();

}

以下是使用您发布的代码和名为person的类的示例。 另请注意,由于所有重复代码,在这种情况下可以使用循环而不是开关。你可以用这个替换你的开关。

public void BloodInformation(){
    int BloodCode;
    for(int i=0; i<BloodTypeArray.length; i++){
        BloodCode = i+1;
        if(this.BloodType == BloodTypeArray[i]){
            System.out.println("Blood Type "+ BloodTypeArray[i] + " & Blood Code is : " + BloodCode + " No of Bags avaliable" );
            return;
        }
    }
    System.out.println("There is no code for the type you write !!");
}