module pind.samples.ja.arithmetic.arithmetic_solution_3; import std.stdio; void main() { while (true) { write("0: Exit, 1: Add, 2: Subtract, 3: Multiply,", " 4: Divide - Please enter the operation: "); int operation; readf(" %s", &operation); // まず、演算を検証しよう if ((operation < 0) || (operation > 4)) { writeln("I don't know this operation"); continue; } if (operation == 0){ writeln("Goodbye!"); break; } // ここに到達したら、4つの演算のうちの1つを // 扱っていることがわかる。ここで、ユーザーから // 2つの整数を読み込む: int first; int second; write(" First number: "); readf(" %s", &first); write("Second number: "); readf(" %s", &second); int result; if (operation == 1) { result = first + second; } else if (operation == 2) { result = first - second; } else if (operation == 3) { result = first * second; } else if (operation == 4) { result = first / second; } else { writeln( "There is an error! ", "This condition should have never occurred."); break; } writeln(" Result: ", result); } }