package simplecalculator;
import java.util.Scanner;
/**
*
* @author anfal
*/
public class SimpleCalculator {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//1.prompt user to input calculation
System.out.print("Enter your operation: ");
//2.read num1, operation, num2.
int num1 = input.nextInt();
char operation = input.next().charAt(0);
int num2 = input.nextInt();
//3.declare and initialize result
double result = 0;
//4.if operation = *, /, +, -
if (operation == '*')
result = num1 * num2;
else if (operation == '/')
result = num1 / num2;
else if (operation == '+')
result = num1 + num2;
else if (operation == '-')
result = num1 - num2;
else
{
System.out.println("Invalid operation.");
System.exit(0);
}
//print result
System.out.println(num1 + " " + operation + " " + num2 + " = " + result); // x * y = result
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.