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("Please input a calculation in the form \"x operation y\": ");
//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;
}
if (operation == '/')
{
result = num1 / num2;
}
if (operation == '+')
{
result = num1 + num2;
}
if (operation == '-')
{
result = num1 - num2;
}
else
{
System.out.println("Invalid input.");
System.exit(0);
}
//print result
System.out.println(num1 + " " + operation + " " + num2 + " = " + 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.