Software Design 2010年7月号《補足》#2: 図解(総天然色版)

小粒ちゃんねる
小粒ちゃんねる #小粒ch

《著》小粒ちゃん@湘南組、小粒ちゃん@博多組

Software Design 連載記事

ここでは、掲載記事に関する補足情報を公開しています。

老頭児(ロートル)エンジニアのつぶやき

ケース1:16進数の文字列表記

  • リスト6 switch文の隘路(その1)
public int hexValue(char ch) throws NonHexDigitException {
    switch (ch) {
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        return (ch - '0');
    case 'a': case 'b': case 'c':
    case 'd': case 'e': case 'f':
        return (ch - 'a') + 10;
    case 'A': case 'B': case 'C':
    case 'D': case 'E': case 'F':
        return (ch - 'A') + 10;
    default:
        throw new NonHexDigitException(ch);
    }
}

ケース2:カードゲーム

  • リスト7 switch文の隘路(その2)
public String toString() {
    StringBuffer buf = new StringBuffer();
    buf.append(suit);

    switch (number) {
    case  1: buf.append("A")   ; break;
    case 10: buf.append("O")   ; break;
    case 11: buf.append("J")   ; break;
    case 12: buf.append("Q")   ; break;
    case 13: buf.append("K")   ; break;
    default: buf.append(number); break;
    }
    return buf.toString();
}

ケース3:n人ジャンケンへの道

  • リスト8 多重if文(Jython版)
def judge(p1, p2):
    if p1.hand == R:
        if p2.hand == R: return None
        if p2.hand == P: return p1
        if p2.hand == S: return p2
    if p1.hand == P:
        ...
    if p1.hand == S:
        ...
  • リスト9 多重論理式(Jython版)
def judge(p1, p2):
    if *1:
        return None
    if ...
        return p1
    if ...
        return p2


TOP


関連記事

  • @

Last updated♪2010/06/22

*1:p1.hand == R and p2.hand == R) or (p1.hand == P and p2.hand == P) or (p1.hand == S and p2.hand == S