계산기 만들기

박천규·2021년 1월 27일
0

Main.class

package practice;

import java.util.*;
public class Main {
	Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Main main = new Main();
		Calculator cal = new Calculator();
		double result;
		main.print("|----------------------계산기입니다--------------------------|");
		main.print("|+, - ,/ ,* 중 원하는 계산을 입력하시면 됩니다. 끝내길 원하시면 q를 입력해주세요 |");
		main.print("|-------------------------------------------------------|");
		double x = main.inputdouble("숫자를 입력해주세요 : ");
		result =x;
		
		while(true) {		
			String select = main.inputString("원하는 계산을 입력해주세요  : ");
			double y =main.inputdouble("숫자를 입력해주세요");
			if(select.equals("+")) {
				x = cal.plus(x, y);
				main.print(result+" "+select+" "+y+" = "+x);
				result = x;
			}
			if(select.equals("-")) {
				x = cal.minus(x, y);
				main.print(result+" "+select+y+" = "+x);
				result = x;
			}
			if(select.equals("*")) {
				x = cal.multiplied(x, y);
				main.print(result+" "+select+y+" = "+x);
				result = x;
			}
			if(select.equals("/")) {
				x = cal.division(x, y);
				main.print(result+" "+select+y+" = "+x);
				result = x;
			}
			
		}

		
		
	}

	
	
	
	void print(String msg) {
		System.out.println(msg);
	}
	
	
	double inputdouble(String msg) {
		System.out.print(msg);
		return sc.nextDouble();
	}
	
	String inputString(String msg) {
		System.out.print(msg);
		return sc.next();
	}
}

Calculator

package practice;

public class Calculator {


	double plus(double x , double y) {
		return x+y;
	}
	
	double minus(double x , double y) {
		return x-y;
	}
	
	double multiplied(double x , double y) {
		return x*y;
	}
	
	double division(double x , double y) {
		return x/y;
	}
}
profile
자바 공부중

0개의 댓글