File #ytqsry76-11791 - JAVA - Sourcecode
Uploaded by zerodeficit - 31/10/2012 16:52 - 82 Views
Source code
import java.util.Scanner;
public class Calculator
{public static void main(String[] args)
{Scanner in = new Scanner(System.in);
char operator = 'z'; //initialize operator
double cValue; //current value
double rhValue; //right hand value
boolean cont = true;
System.out.print("Enter starting value: ");
cValue = in.nextDouble();
while(true)
{System.out.print("Select an operator (+, -, *, /), 'c' or 'C' to clear, or 'q' to quit: ");
String temp = in.nextLine();
char tempOperator = temp.charAt(0);
if (tempOperator == 'c' || tempOperator == 'C')
{cValue = 0.0;
System.out.println("Current value is: " + cValue);
System.out.println();
}else if(tempOperator == 'q')
{System.out.println("Final result: " + cValue);
System.exit(1);
}else if(tempOperator == '+' || tempOperator == '-' || tempOperator == '*' || tempOperator == '/')
{operator = tempOperator;
break;
}elsethrow new IllegalArgumentException("operator not valid");
}System.out.println("Enter a right hand value (type double): ");
rhValue = in.nextDouble();
System.out.println("Math expression: answer " + operator + "= " + rhValue);
switch(operator)
{case '+': cValue =+ rhValue;
break;
case '-': cValue =- rhValue;
break;
case '*': cValue = cValue * rhValue;
break;
case '/': cValue = cValue / rhValue;
break;
}System.out.println("Current value is: " + cValue);
}}
