카타 8급 Basic Mathematical Operations

Jmnote (토론 | 기여)님의 2019년 1월 27일 (일) 23:46 판 (→‎Java)

1 개요

카타 8급 C
# 🔗 문제 풀이

틀:카타 8급-2

2 Java

public class BasicOperations
{
  public static Integer basicMath(String op, int v1, int v2)
  {
    switch(op) {
      case "+": return v1+v2;
      case "-": return v1-v2;
      case "*": return v1*v2;
      case "/": return v1/v2;
    }
    return 0;
  }
}
public class BasicOperations
{
  public static Integer basicMath(String op, int v1, int v2)
  {
  switch (op) {
    case "-": return v1 - v2;
    case "+": return v1 + v2;
    case "*": return v1 * v2;
    case "/": {
      if (v2 == 0) throw new IllegalArgumentException("Division by zero");
      return v1 / v2;
    }
    default:
      throw new IllegalArgumentException("Unknown operation: " + op);
    }
  }
}

3 PHP

function basicOp($op, $val1, $val2)
{
  switch($op) {
  case '+': return $val1+$val2;
  case '-': return $val1-$val2;
  case '*': return $val1*$val2;
  case '/': return $val1/$val2;
  }
}

4 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}