2017-11-07 8 views
-1

ifステートメントを別の方法に変換するのが難しいです。私はそれを4つの別々のメソッドに変換しようとしています:add、sub、mul、およびdiv、CMDに記述する必要がありますが、何とかできません。私は助けと感謝が必要です。Java:ifステートメントからメソッドへの変換

public static void main(String[] args){ 
    int first = Integer.parseInt(args[1]); 
    int second = Integer.parseInt(args[2]); 
    int third = Integer.parseInt(args[3]); 
    int forth = Integer.parseInt(args[4]); 

    if(args[0].equals("add")){ 
     if (second == 0 || forth == 0){ 
      System.out.println("Undividable by zero"); 
     } else if (second == forth){ 
      System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth); 
     } else { 
      System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first * forth + second * third) + "/" + (second * forth)); 
     } 
    } else if (args[0].equals("sub")){ 
     if (second == 0 || forth == 0){ 
      System.out.println("Undividable by zero"); 
     } else if (second == forth){ 
      System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first + third) + "/" + forth); 
     } else { 
      System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " + (first * forth - second * third) + "/" + (second * forth)); 
     } 
    } else if (args[0].equals("mul")){ 
     if (second == 0 || forth == 0){ 
      System.out.println("Undividable by zero"); 
     } else { 
      System.out.println(first + "/" + second + " * " + third + "/" + forth + " = " + (first * third) + "/" + (second * forth)); 
     } 
    } else if (args[0].equals("div")){ 
     if (second == 0 || forth == 0){ 
      System.out.println("Undividable by zero"); 
     } else { 
      System.out.println(first + "/" + second + " : " + third + "/" + forth + " = " + (first * forth) + "/" + (second * third)); 
     } 
    } else { 
     System.out.println("Recheck the code"); 
    }   
} 
+1

なぜ4つのメソッドを作成し、 'args [0]'の値に応じて正しいメソッドを呼び出せませんか? – tilper

+0

@tilperそれはまさに彼がやろうとしていることです。 –

+0

これは私の宿題であり、add、sub、mul、divなどの演算子はメソッド –

答えて

0

それとも、一つの方法 公共ボイドthis_is_your_method(int型のn){...}

を作りたい場合は、作るとき、私は、問題あなたがたが原因のSystem.out.printlnであると仮定していますあなたはあなたがあなたが何かを印刷したくないのであなたが言っているものを印刷したいので、あなたが戻そうとしているタイプを述べる必要があります。これを印刷したいだけです。そのコードを一度書くだけで何度も良いのですが、args [0]をargs [n]に変更するだけです。

0

Main.Class

public static void main(String[] args) { 
    int first = Integer.parseInt(args[1]); 
    int second = Integer.parseInt(args[2]); 
    int third = Integer.parseInt(args[3]); 
    int forth = Integer.parseInt(args[4]); 

    if(args[0].equals("add")){ 
     add(first,second,third,forth); 
    } ... 
} 

public static void add(int first, int second, int third, int forth) { 
    if (second == 0 || forth == 0){ 
     System.out.println("Undividable by zero"); 
    } else if (second == forth){ 
     System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth); 
    } else { 
     System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " + (first * forth + second * third) + "/" + (second * forth)); 
    } 
} 
0

あなたはまた、スイッチケースを選ぶことができ、その後、他の場合が多いとき。ここで

は、私はまた、あなたがラムダ関数(Javaの8)を使用して、それを行うことができますどのように共有しています...あなたはメソッドにコードをモジュール化することができますどのように

です。

interface MathOperation { 
    public void operate(int first, int second, int third, int forth); 
} 

public class StackOverflow { 

    public static void main(String[] args) { 
     int first = Integer.parseInt(args[1]); 
     int second = Integer.parseInt(args[2]); 
     int third = Integer.parseInt(args[3]); 
     int forth = Integer.parseInt(args[4]); 

     MathOperation addition = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else if (b == d) System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + (a + c) + "/" + d); else System.out.println(a + "/" + b + " + " + c + "/" + d + " = " + (a * d + b * c) + "/" + (b * d));}; 
     MathOperation subtraction = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else if (b == d) System.out.println(a + "/" + b + " - " + c + "/" + d + " = " + (a + c) + "/" + d); else System.out.println(a + "/" + b + " - " + c + "/" + d + " = " + (a * d - b * c) + "/" + (b * d));}; 
     MathOperation multiplication = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else System.out.println(a + "/" + b + " * " + c + "/" + d + " = " + (a * c) + "/" + (b * d));}; 
     MathOperation division = (a, b, c, d) -> { if (b == 0 || d == 0) System.out.println("Undividable by zero"); else System.out.println(a + "/" + b + " : " + c + "/" + d + " = " + (a * d) + "/" + (b * c));}; 

     switch(args[0]) { 
      case "add" : 
       add(first, second, third, forth); 
       addition.operate(first, second, third, forth); 
       break; 
      case "sub": 
       sub(first, second, third, forth); 
       subtraction.operate(first, second, third, forth); 
       break; 
      case "div": 
       div(first, second, third, forth); 
       division.operate(first, second, third, forth); 
       break; 
      case "mul": 
       multiply(first, second, third, forth); 
       multiplication.operate(first, second, third, forth); 
       break; 
      default : 
       System.out.println("Recheck the code"); 
     } 
    } 

    private static void multiply(int first, int second, int third, int forth) { 
     if (second == 0 || forth == 0) { 
      System.out.println("Undividable by zero"); 
     } else { 
      System.out.println(first + "/" + second + " * " + third + "/" + forth + " = " + (first * third) + "/" 
        + (second * forth)); 
     } 
    } 

    private static void div(int first, int second, int third, int forth) { 
     if (second == 0 || forth == 0) { 
      System.out.println("Undividable by zero"); 
     } else { 
      System.out.println(first + "/" + second + " : " + third + "/" + forth + " = " + (first * forth) + "/" 
        + (second * third)); 
     } 
    } 

    private static void sub(int first, int second, int third, int forth) { 
     if (second == 0 || forth == 0) { 
      System.out.println("Undividable by zero"); 
     } else if (second == forth) { 
      System.out.println(
        first + "/" + second + " - " + third + "/" + forth + " = " + (first + third) + "/" + forth); 
     } else { 
      System.out.println(first + "/" + second + " - " + third + "/" + forth + " = " 
        + (first * forth - second * third) + "/" + (second * forth)); 
     } 
    } 

    private static void add(int first, int second, int third, int forth) { 
     if (second == 0 || forth == 0) { 
      System.out.println("Undividable by zero"); 
     } else if (second == forth) { 
      System.out.println(
        first + "/" + second + " + " + third + "/" + forth + " = " + (first + third) + "/" + forth); 
     } else { 
      System.out.println(first + "/" + second + " + " + third + "/" + forth + " = " 
        + (first * forth + second * third) + "/" + (second * forth)); 
     } 
    } 
} 
関連する問題