티스토리 뷰
[Java] Exercise: Simple Calculator Program (자바로 간단한 계산기 구현하기)
나수비니 2023. 10. 4. 22:46Overview
I have created a simple calculator program in Java that can perform basic operations like addition, subtraction, multiplication, and division. These operations are performed using a switch case statement in Java.
자바로 간단한 계산기 프로그램을 만들었다. 덧셈, 뺄셈, 곱하기, 나누기와 같은 기본 연산을 수행할 수 있다. 이 연산은 자바의 switch case문으로 구현된다.
Program Description
The user should input two numbers. After that the program will ask which operation to perform. Based on the user’s choice, arithmetic operations will be performed and the result will be displayed on the console.
사용자는 두 개의 숫자를 입력해야 한다. 이후, 프로그램은 어떤 연산을 수행할 것인지 묻는다. 사용자의 선택에 따라 산술 연산이 수행되고, 결과가 콘솔에 표시된다.
Procedure
Step 1
Get the operation to be performed. (+, -, *, /)
사용자로부터 수행할 산술 연산을 얻는다.
public class Main {
public static void main(String[] args) {
char operator;
Double num1 = 0.0;
Double num2 = 0.0;
Scanner sc = new Scanner(System.in);
System.out.println("Choose an operator: +, -, *, /");
operator = sc.next().charAt(0);
Step 2
Get two numbers from the user.
사용자로부터 두 개의 숫자를 입력 받는다.
System.out.println("Enter first number");
num1 = sc.nextDouble();
System.out.println("Enter second number");
num2 = sc.nextDouble();
Step 3
Using Scanner, the operator values that the user selected are passed to the switch case statement.
Scanner를 이용해 사용자가 선택한 산술 연산을 Swtich case 문으로 전달한다.
Step4
When the operator value matches one of the case conditions, the statement is executed.
연산자 값이 case 조건 중 하나와 일치할 때, 해당 문이 실행된다.
switch(operator) {
case '+' :
{
double addition = num1 + num2;
System.out.println("Total after Addition is: " + addition);
break;
}
case '-' :
{
double subtraction = num1 - num2;
System.out.println("Total after Subtraction is: " + subtraction);
break;
}
case '*' :
{
double multiplication = num1 * num2;
System.out.println("Total after multiplication is: " + multiplication);
break;
}
case '/' :
{
double division = num1 / num2;
System.out.println("Total after division is: "+ division);
break;
}
default :
{
System.out.println("Please select operator");
return;
}
}
sc.close();
}
}
Step5
The result is calculated and displayed on the console.
결과값이 계산되고 콘솔에 표시된다.
Output
Source Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char operator;
Double num1 = 0.0;
Double num2 = 0.0;
//create an object of Scanner class
Scanner sc = new Scanner(System.in);
//ask users to enter operator
System.out.println("Choose an operator: +, -, *, /");
operator = sc.next().charAt(0);
//ask users to enter numbers
System.out.println("Enter first number");
num1 = sc.nextDouble();
System.out.println("Enter second number");
num2 = sc.nextDouble();
switch(operator) {
case '+' :
{
double addition = num1 + num2;
System.out.println("Total after Addition is: " + addition);
break;
}
case '-' :
{
double subtraction = num1 - num2;
System.out.println("Total after Subtraction is: " + subtraction);
break;
}
case '*' :
{
double multiplication = num1 * num2;
System.out.println("Total after multiplication is: " + multiplication);
break;
}
case '/' :
{
double division = num1 / num2;
System.out.println("Total after division is: "+ division);
break;
}
default :
{
System.out.println("Please select operator");
return;
}
}
sc.close();
}
}
My next goal is implement it in a GUI 🥴
'Programming Language > JAVA' 카테고리의 다른 글
[Java] Modifier (1) | 2023.10.10 |
---|---|
[Java] Vector (1) | 2023.10.10 |
[Java] next(), nextLine() (0) | 2023.10.03 |
[Java] GUI 기초, Swing과 AWT (0) | 2023.10.02 |
[Java] 랜덤게임 만들기 (0) | 2023.10.01 |