재대후 처음 자바를 공부하면서 만든 간단한 Java 계산기 프로그램
//Main
package calculator;
import java.util.Scanner;
public class CalMain {
public static void main(String[] args) {
double num1;
double num2;
double result;
Cal cal= new Cal();
Scanner input=new Scanner(System.in);
String c="@";
System.out.println("숫자를 입력해 주세요 :\t");
num1 = input.nextDouble();
do{
if(c=="#")
break ;
System.out.println("연산자 :\t");
c=input.next();
System.out.println("숫자를 입력해주세요\t");
num2=input.nextDouble();
System.out.println("결과값 :\t");
switch(c){
case "+":
result = cal.add(num1, num2);
System.out.println(result);
num1=result;
break;
case "-":
result = cal.minus(num1, num2);
System.out.println(result);
num1=result;
break;
case "*":
result = cal.multi(num1, num2);
System.out.println(result);
num1=result;
break;
case "/":
result = cal.division(num1, num2);
System.out.println(result);
num1=result;
break;
case "c":
System.out.println("초기화\t");
System.out.println("숫자를 입력해주세요\t");
num1 = input.nextDouble();
break;
case "#":
System.out.println("끝\t");
c="#";
break;
default:
System.out.println("잘못된 연산입니다 연산자부터 다시 입력해 주세요\t");
break ;
}
}while(true);
}
}
//////////////////////////////////////////////////////////////////////////
//Class
package calculator;
public class Cal {
double result;
double add(double num1, double num2){
result = num1 + num2;
return result;
}
double minus(double num1, double num2){
result = num1 - num2;
return result;
}
double multi(double num1, double num2){
result = num1 * num2;
return result;
}
double division(double num1, double num2){
result = num1 / num2;
return result;
}
double clear(){
result = 0;
return result;
}
}
////////////////////////////////////////////////////////////////////
뼈대만 만들었다 자잘한건 아직....