Java中的Switch语句 [英] Switch Statement in Java

查看:84
本文介绍了Java中的Switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是JOptionPane.showInputDialog程序的类,其创建目的是将字符串作为输入(单词或#s)并将其转换为基于传统按键式电话小键盘的数字电话号码,例如输出消息.好的,这里是程序的类(实际的JOptionPane主程序代码与该主题并不真正相关,因此我未将其包括在内):

This is a class for a JOptionPane.showInputDialog program, that is created in order to take a string of characters as input (words or #s) and convert it to a numeric phone number based on the traditional touch tone telephone keypad as output message. Okay here is the class for the program (actual JOptionPane main program code is not really relevant to this topic, so I did not include it):

//Letters Q & Z to be omitted to mimic old days phone key letters
public class Phone
{
  //private data
  private String word = "";
  private String number = "";
  
  //constructor
  public Phone(String word)
  {
    this.word = word;
    setNumber();
  }
  
  //set method
  public void setNumber()
  {
    for(int i = 0; (i < word.length() && i < 7); i++)
    {
      word.toLowerCase();
      char c = word.charAt(i);
      switch(c)
      {
        case '0': number += "0"; break;
        case '1': number += "1"; break;
        case '2': number += "2"; break;
        case '3': number += "3"; break;
        case '4': number += "4"; break;
        case '5': number += "5"; break;
        case '6': number += "6"; break;
        case '7': number += "7"; break;
        case '8': number += "8"; break;
        case '9': number += "9"; break;
      }
    }
    if(number.length() > 3)
      number = number.substring(0,3) + "-" + number.substring(3);
    else
        number = number;
  }
  
  //toString method
  public String toString()
  {
    return number;
  }
}



我想知道如何在上一类中针对案例2-9使用此switch语句,以不仅检查电话按键式小键盘上的数值,而且还要检查三个字母字符中的每个字符.例如,我真的很希望能够执行这样的操作,但是它不会让我在java中执行操作:



I want to know how to use this switch statement in the above class for cases 2 - 9 to not only check for the numerical values on a phone touch tone keypad but also each of the three letter chars as well. For example I really want to be able to do something like this, but it will not let me do it in java:

//Letters Q & Z to be omitted to mimic old days phone key letters
public class Phone
{
  //private data
  private String word = "";
  private String number = "";
  
  //constructor
  public Phone(String word)
  {
    this.word = word;
    setNumber();
  }
  
  //set method
  public void setNumber()
  {
    for(int i = 0; (i < word.length() && i < 7); i++)
    {
      word.toLowerCase();
      char c = word.charAt(i);
      switch(c)
      {
        case '0': number += "0"; break;
        case '1': number += "1"; break;
        case '2', 'a', 'b', 'c': number += "2"; break;
        case '3', 'd', 'e', 'f': number += "3"; break;
        case '4', 'g', 'h', 'i': number += "4"; break;
        case '5', 'j', 'k', 'l': number += "5"; break;
        case '6', 'm', 'n', 'o': number += "6"; break;
        case '7', 'p', 'r', 's': number += "7"; break;
        case '8', 't', 'u', 'v': number += "8"; break;
        case '9', 'w', 'x', 'y': number += "9"; break;
      }
    }
    if(number.length() > 3)
      number = number.substring(0,3) + "-" + number.substring(3);
    else
        number = number;
  }
  
  //toString method
  public String toString()
  {
    return number;
  }
}



现在,我想知道是否有可能使用switch语句(在上面的第二个书面代码中)让Java做我不允许做的事情(基本上检查一下是否有任何字符符合当前情况).我已经知道我可以毫无疑问地使用if语句来做到这一点,我想除了事实,我认为这将不仅仅是编写switch语句,而是编写更多的代码.我只是想基本了解为什么我无法获得switch语句或语法来解决此问题,因此



Now, I want to know if it is possible to do what Java will not let me do in the above second written code using a switch statement (basically check to see if any of the chars match the case at hand). I already know I could use a if statement to do this with no problem I imagine other than the fact that I think it would be more code writing than just using a switch statement. I just want to know basically why I can''t get the switch statement or syntax for this matter to work & compile in Java like I want.

推荐答案

是的:只要使大小写符合以下条件即可:

Yes: just make the cases fall through:

case '0': number += "0"; break;
case '1': number += "1"; break;
case '2':
case 'a':
case 'b':
case 'c': number += "2"; break;


这篇关于Java中的Switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆