2011-01-02 13 views
1

私は次のコードを簡素化したい:簡素化if文、Javaの

public static void main(String[] args) { 

     int c1 = Integer.parseInt(args[0]); 
     int c2 = Integer.parseInt(args[1]); 
     int c3 = Integer.parseInt(args[2]); 

     /* 1 */if (c2 - c1 == 0) { 
      /* 2 */if (c1 != c3) { 
       c3 += c1; 
       /* 4 */System.out.println(c3); 
       /* 5 */c3 *= c2; 
       /* 6 */} 
     } 

     /* 7 */if (c1 == c3) 
      /* 8 */if (c1 - c2 == 0) 
      /* 9 */{ 
       c3 += c1; 
       /* 10 */System.out.println(c3); 
       /* 11 */c3 *= c1; 
       /* 12 */if (c1 < c2) 
        c2 += 7; 
       /* 13 */else 
        c2 += 5; 
       /* 14 */} 

     /* 15 */System.out.println(c1 + c2 + c3); 
    } 

まずc2 - c1 == 0は同じであることはc1 == c2ようですc2 == c1c1 - c2 == 0と同じです。私はif (c1 < c2)を削除して、else文の内容を保持することができます。

結果:

public static void main(String[] args) { 

     int c1 = Integer.parseInt(args[0]); 
     int c2 = Integer.parseInt(args[1]); 
     int c3 = Integer.parseInt(args[2]); 

     /* 1 */if (c1 == c2) { 
      /* 2 */if (c1 != c3) { 
       c3 += c1; 
       /* 4 */System.out.println(c3); 
       /* 5 */c3 *= c2; 
       /* 6 */} 
     } 

     /* 7 */if (c1 == c3) 
      /* 8 */if (c1 == c2) 
      /* 9 */{ 
       c3 += c1; 
       /* 10 */System.out.println(c3); 
       /* 11 */c3 *= c1; 

        c2 += 5; 
       /* 14 */} 

     System.out.println(c1 + c2 + c3); 
    } 

私の質問は今、単純化することができるものでしょうか?インナーが外にあるなら、私はインフォーメーションを単純化することができます。どう思いますか?

+2

好奇心が強い。これは何のため? –

+1

* "あなたはどう思いますか?" *インデントと行番号のコメントでは、そのコードはほとんど読めなくなり、おそらくこの質問への回答が妨げられていると思います。コードを読み取り可能にします(通常のインデントと右端の数字を使用してください)、あなたはもっと運があるかもしれません。 –

+0

@エヴァン:はい、宿題やテストのようです。 –

答えて

4

警告:コードの2番目のバージョンが正しいと仮定しました。つまり、最初の単純化が正しいと思います。

if文の混乱はにまで単純化することができる。

  • ファクターを一般的な状態から1および8に:あなたは、基準点として、これは私が単純化するものである/* line numbers /*使用

    if (c1 == c2) { 
        c3 += c1; 
        System.out.println(c3); 
        if (c1 != c3) { 
        c3 *= c2; 
        } else { 
        c2 += 5; 
        c3 *= c1; 
        } 
    } 
    

  • 共通コードを4と10の倍数で除算します。
2

ifで条件演算子をいくつか使用してみてください。&&を実行できる場合は、それらをネストする理由はありません。

if (c1 == c2 && c1 != c3) { 
    ... 
} 

if (c1 == c2 && c2 == c3) { 
    ... 
} 
+0

downvoteの理由は? –

+0

おそらく誰かが条件演算子が何であるか理解していないからです。 :/ – marcog

+0

@marcogおそらく、さらに簡素化(つまり、あなたの答え)はそれらを必要としませんが、私の答えはおそらく駄目でしょう。 :) –

0
public static void main(String[] args) 
{ 
    int c1 = Integer.parseInt(args[0]); 
    int c2 = Integer.parseInt(args[1]); 
    int c3 = Integer.parseInt(args[2]); 

    if(c1 == c2) 
    { 
     c3 += c1; 
     System.out.println(c3); 

     if(c1 == c3) 
     { 
      c3 *= c1; 
      c2 += 5; 
     } 
     else 
     { 
      c3 *= c2; 
     }  
    } 
    System.out.println(c1 + c2 + c3); 
} 
0
public static void main(String[] args) { 

int c1 = Integer.parseInt(args[0]); 
int c2 = Integer.parseInt(args[1]); 
int c3 = Integer.parseInt(args[2]); 

if (c2 == c1) 
    { 
     if(c1!= c3) 
     { 
      c3 += c1; 
      System.out.println(c3); 
      c3 *= c2; 
     } 
     else { 
      c3 += c1; 
      System.out.println(c3); 
      c3 *= c1; 
      c2 += 5; 
     } 
    } 
System.out.println(c1 + c2 + c3); 

}

0

あなたはif文を考慮することはできません。それらは順次実行され、最初のものは2番目のものの値を変更します。結果が変わります。