mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
Merge pull request #86 from Weltenbummler397/exercism-sync/2e121aaacf2b47cc
[Sync Iteration] java/calculator-conundrum/1
This commit is contained in:
commit
06fcc7ca81
@ -0,0 +1,18 @@
|
||||
class CalculatorConundrum {
|
||||
public String calculate(int operand1, int operand2, String operation) {
|
||||
switch (operation) {
|
||||
case null : throw new IllegalArgumentException("Operation cannot be null");
|
||||
case "" : throw new IllegalArgumentException("Operation cannot be empty");
|
||||
case "+" : return operand1 + " + " + operand2 + " = " + (operand1 + operand2);
|
||||
case "*" : return operand1 + " * " + operand2 + " = " + (operand1 * operand2);
|
||||
case "/" :
|
||||
try {
|
||||
return operand1 + " / " + operand2 + " = " + (operand1 / operand2);
|
||||
} catch (ArithmeticException e) {
|
||||
throw new IllegalOperationException("Division by zero is not allowed", e);
|
||||
}
|
||||
default: throw new IllegalOperationException(String.format("Operation '%s' does not exist", operation));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user