Java programming basics practice-switch statement

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

problem

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

Implementation

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;
	}

}

Question

Impressions

Recommended Posts

Java programming basics practice-switch statement
Java programming basics practice-for statement
java programming basics
Java programming basics practice-array
Java basics
Java basics
Java basics
Java JAR basics
Object-oriented (Java) basics
java Generic Programming
Java concurrency basics
Java for statement
Basics of java basics ② ~ if statement and switch statement ~
Java switch statement
[Java] for statement, while statement
Constraint programming in Java
Java instruction execution statement
Java programming (class method)
Java Network Basics (Communication)
(Memo) Java for statement
Basics of try-with-resources statement
Java programming (class structure)
All about Java programming
java competitive programming memo
Muscle Java Basics Day 1
Java Programming Thread Runnable
Amazing Java programming (let's stop)
Basics of character operation (java)
[Java] Basic statement for beginners
Java programming (variables and data)
☾ Java / Iterative statement and iterative control statement
Summary of Java language basics
Getting Started with Java Basics
Java Development Basics ~ Exercise (Array) ~
[Java11] Stream Usage Summary -Basics-
[Java basics] What is Class?
[Java] Basic terms in programming
Collection of programming selection tasks to make and remember (Java basics)
Competitive programming private cheat sheet (Java)
Java Functional Programming Exercise Book --zipWith-
Introduction to Functional Programming (Java, Javascript)
Java learning memo (while statement, do-while statement)
[Java] Really scary switch statement story
Java study # 4 (conditional branching / if statement)
Summary of object-oriented programming using Java
Java Performance Chapter 5 Garbage Collection Basics