1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
-
-
- /**
- * Basic class to perform basic functions for our Graphing Calculator
- * (+, -, *, /)
- * Lauren Green
- * 10/19/18
- */
- public class Basic
- {
- int input1 = 0;
- int input2 = 0;
- int answer;
-
- public Basic()
- {
- //a check to make sure user input is valid.
- boolean validInput = false;
-
- while(!validInput) {
- //Get input of first number
- input1 = Console.getIntegerInput("What is the first number?");
-
- //Get input of operation
- String operation = Console.getStringInput("Which operation would you like to perform? \n +, -, *, /");
-
- //Get input of second number
- input2 = Console.getIntegerInput("What is the second number?");
-
- //Do the math depending on the inputs.
- //addition
- if (operation.equals("+")) {
- answer = input1 + input2;
- Console.println(Double.toString(answer));
- validInput = true;
-
- //subtraction
- } else if (operation.equals("-")) {
- answer = input1 - input2;
- Console.println(Double.toString(answer));
- validInput = true;
-
- //multiplication
- } else if (operation.equals("*")) {
- answer = input1 * input2;
- Console.println(Double.toString(answer));
- validInput = true;
-
- //division
- } else if (operation.equals("/")) {
- //error message when trying to divide by 0.
- if (input2 == 0) {
- Console.println("undefined");
- //Insert return to main menu.
- } else {
- answer = input1 / input2;
- Console.println(Double.toString(answer));
- validInput = true;
- }
- } else {
- //error if wrong input was entered
- Console.println("Error, try again \n");
-
- }
-
- }
- //Insert return to main menu.
- }
- }
-
-
|