Created
October 14, 2012 18:28
-
-
Save benfb/3889398 to your computer and use it in GitHub Desktop.
CalculatorRunner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class CalculatorRunner | |
{ | |
public static void main(String[] args) | |
{ | |
int num1; | |
int num2; | |
char operator; | |
boolean valid; | |
//instantiate a Scanner | |
Scanner scan = new Scanner(System.in); | |
//prompt the user for the operator (how do you read in a character?) | |
System.out.println("Enter the operator:"); | |
operator = scan.next().charAt(0); | |
//setup a set of conditions below to determine if the operator is valid | |
if(operator == '+' || operator == '-' || operator == '*' || operator == '/') // if the operator is valid, set boolean valid = true | |
{ | |
valid = true; | |
} //else, it stops the program | |
//get the remaining inputs from the user | |
System.out.println("Enter the first operand:"); | |
num1 = scan.nextInt(); | |
System.out.println("Enter the second operand:"); | |
num2 = scan.nextInt(); | |
//check for division by zero | |
if(num2 == 0 && operator == '/') //if the operator is 0 and the second number is 0, print a statement | |
{ | |
System.out.println("You can't divide by zero."); | |
valid = false; | |
} | |
else{ valid=true; } //if not dividing by 0, valid is true | |
//instantiate Calculator object with the values entered by the user | |
if(valid == true) //only if valid = true, if not, the program stops. | |
{ | |
Calculator calculator = new Calculator(num1, num2, operator); | |
System.out.println(calculator.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment