A, B, and C map to 2 
D, E, and F map to 3 
G, H, and I map to 4 
J, K, and L map to 5 
M, N, and O map to 6 
P, R, and S map to 7 
T, U, and V map to 8 
W, X, and Y map to 9 
 
比如输入A或B或C 都转换成2 怎么实现啊???

解决方案 »

  1.   

    使用一个map以字母为key数字为value就行了。
      

  2.   


    Map<String,Integer> map=new HashMap<String,Integer>();
    String[] arr={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","R","S","T","U","V","W","X","Y"};
    for(int i=0;i<arr.length;i++){
    map.put(arr[i], (i/3)+2);
    }
    Scanner in=new Scanner(System.in);
    while(true){
    System.out.println("请输入字母");
    String str=in.nextLine();
    if(str.matches("[A-Z]")&&!str.equals("Q")&&!str.equals("Z")){
    System.out.println(str+"的对应值是:"+map.get(str));
    }else{
    System.out.println("输入错误,请重新输入!");
    }

    }
      

  3.   

    Map<字母,对应的值>优点:初始化后无需判断,直接get即可。
      

  4.   


    //A 
    int charAZ = 65;
    Map map = new HashMap();
    int value = 2;
    int count = 0;for(int i = 0;i<26;i++){
         map.put(char(charAZ)+"" , value + count/3 );
         count++;}
      

  5.   

    A-Y 对应ASCII码 65到。。
    于是我们可以这么算设输入值为X
    (X-65)/3 + 2 得到的就是输出值。