Challenge: Write a program that implements a simple calculator of integer arithmetic operations (+, -, *, /). The input from the user would be three values: an operand, an operator, and an operand. Once the three values are entered, the program performs the calculation. Use a SWITCH to implement this exercise -the selector will be the operator.
import java.util.Scanner;
public class lab5_4
{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter three values I.E: a + b");
int a=in.nextInt();
char c=in.next().charAt(0);
int b=in.nextInt();
switch(c)
{
case '+':
int sum= a+b;
System.out.println("The sum of " + a + " and " + b + " is equal to: " + sum);
break;
case '*':
int product=a*b;
System.out.println("The product of " + a + " and " + b + " is equal to: " + product);
break;
case '/':
double division=a/b;
System.out.println("The division of " + a + " by " + b + " is equal to: " + division);
break;
case '-':
int rest=a-b;
System.out.println("The rest of " + a + " by " + b + " is equal to: " + rest);
break;
default: System.out.println("Input Error!");
}
}
}
No comments:
Post a Comment