一个三位数字(如:102),
------------
封装成一个类
class MyClass {
int first;
int second;
in third;
}

解决方案 »

  1.   

    haroyy(天平) 的实现是一种很好的思想!
    可以考虑考虑
      

  2.   

    应该使用按位与的方法。
    public class BitOperator
    {
    private String _strValue = "";
    public String getValue()
    {
    return _strValue;
    }
    public void setValue(String strValue)
    {
    _strValue = strValue;
    } public String getResult()
    {
    String strResult = "";
    for (int i = 0; i < _strValue.length(); i++)
    {
    char ch = _strValue.charAt(i);
    if (ch < '0' || ch > '2')
    {
    strResult += "未知";
    continue;
    }
    switch(ch & 3)
    {
    case 0 : strResult += "红";  break;
    case 1 : strResult += "黄";  break;
    case 2 : strResult += "绿";  break;
    case 3 : strResult += "未知";  break;
    default : strResult += "未知";
    } }
    return strResult;
    } public static void main(String [] args)
    {
    BitOperator test = new BitOperator();
    test.setValue("0ab$%&99211");
            System.out.println(test.getResult()); }
    }
      

  3.   

    把三位数作为字符串处理
    public class Test
    {
    public static void main(String[] args)
    {
    String s=args[0];
    int r=0,y=0,g=0;
    for(int i=0;i<s.length();i++)
    {
    switch(s.charAt(i))
    {
    case '0':r++;break;
    case '1':y++;break;
    case '2':g++;break;
    }

    }
    System.out.println(r+"红"+y+"黄"+g+"绿");
    }
    }
      

  4.   

    感谢大家的参与,想不到有这么多好心人。
    不过,只有 haroyy(天平) 的思路是符合我的要求的,非常感谢您!