解决java switch……case不能匹配字符串的方法

 enum Animal {  
     dog,cat,bear;
      public static Animal getAnimal(String animal){  
        return valueOf(animal );  
     }   
 }

 public class Client {  
	  
    public void caseAnimal(String animal){  
        switch(Animal.getAnimal(animal)){  
        case cat:  
            System.out.println("this is a cat");  
            break;  
        case dog:  
            System.out.println("this is a dog");  
            break;  
        case bear:  
            System.out.println("this is a bear");  
            break;  
        }  
    }
    
   public static void main(String[] args) {
  	  Client client = new Client();  
        client.caseAnimal("cat"); 
	} 
  
 } 

 

public void switchCaseStr() {
  
  Map map=new HashMap();
  
  map.put("hello", 1);
  map.put("haha", 2);
  map.put("yes", 3);
  map.put("in", 4);
  
 
  String str="hello";
  
  switch(map.get(str))
  {    
   case 3:
        System.out.println("yes");
       break;
   case 1:
        System.out.println("hello");
        break;
    case 2:
        System.out.println("haha");
       break;
    case 4:
       System.out.println("in");
       break;
  
   default:
    System.out.println("default");
  }
 }



 

你可能感兴趣的:(java,web开发)