Is it easy to write the program I made when I was a newcomer now in my second year, or have I forgotten it? I thought, so I made it.
Conclusion
Please enter the first number>
10
Please enter the second number>
3
Please select the calculation method>
(a)Addition
(b)Subtraction
(c)Multiply
(b)division
d
The answer is 3
I made it as follows.
Check22.java
package practice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Check22 {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
int num1 = 0;
int num2 = 0;
try {
System.out.println("Please enter the first number");
line = reader.readLine();
num1 = Integer.parseInt(line);
System.out.println("Please enter the second number");
line = reader.readLine();
num2 = Integer.parseInt(line);
System.out.println("Please select the calculation method");
System.out.println("(a)Addition");
System.out.println("(b)Subtraction");
System.out.println("(c)Multiply");
System.out.println("(d)division");
line = reader.readLine();
if (!( line.equals("a")) && !( line.equals("b") )&& !( line.equals("c") )&& !( line.equals("d"))) {
System.out.println("a,b,c,Please enter one of d");
}else {
String symbol = line;
int answer = calc(num1,num2,symbol);
System.out.println("The answer is" + answer + "is.");
}
}catch(NumberFormatException e) {
System.out.println("Please enter a number");
}catch (IOException e) {
e.printStackTrace();
}
}
public static int calc(int num1, int num2, String symbol) {
int result;
switch(symbol) {
case "a":
result = num1 + num2;
break;
case "b":
result = num1 - num2;
break;
case "c":
result = num1 * num2;
break;
default:
result = num1 / num2;
break;
}
return result;
}
}
Recommended Posts