Build a simple “English Language” calculator that does the following:
● Takes three inputs from the keyboard
● Two of the inputs are single-digit numbers (0 to 9)
● The third input is a char from the keyboard, representing one of the five operations from the keyboard:
○ + (addition)
○ - (subtraction)
○ * (multiplication)
○ / (division)
○ ^ (exponentiation)
● Output the description of the operation in plain English, as well as the numeric result
import java.io.IOException;
import java.util.Scanner;
import static java.lang.Math.pow;
public class Calc {
public static void main(String[] args) throws IOException {
Scanner scan=new Scanner(System.in);
System.out.print("Enter first number(0 to 9): ");
int fNumb=scan.nextInt();
System.out.print("Enter second number(0 to 9): ");
int sNumb=scan.nextInt();
System.out.println("Choose one of operation: +, -, *, /, ^. ");
char oper=(char)System.in.read();
switch(oper){
case '+':
System.out.println("Result of addition: "+(fNumb+sNumb));
break;
case '-':
System.out.println("Result of subtraction: "+(fNumb-sNumb));
break;
case '*':
System.out.println("Result of multiplication: "+(fNumb*sNumb));
break;
case '/':
System.out.println("Result of division: "+ ((double) fNumb/(double) sNumb));
break;
case '^':
System.out.println("Result of exponentiation: "+(pow(fNumb,sNumb)));
break;
}
}
}
Comments
Leave a comment