Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * Author: Benjamin San Nicolas
- * Course: CSC240C41
- * Date: October 17, 2018
- *
- * Purpose: The purpose of this program is to request two digits and a operator
- * (+,-,*,/) then output the answer after the last item is entered.
- ******************************************************************************/
- //Header
- import java.util.Scanner;
- //Define class
- public class javaCalc
- {
- //Main Method
- public static void main(String[] args)
- {
- //Variables
- int num1, num2;
- float ans;
- char operation;
- //Scanner
- Scanner input = new Scanner(System.in);
- //Request operation
- do
- {
- System.out.print("1) Addition\n");
- System.out.print("2) Subtraction\n");
- System.out.print("3) Mutiplication\n");
- System.out.print("4) Division\n");
- System.out.print("5) Exit Program\n");
- System.out.print("Please choose an operation: ");
- operation = input.next().charAt(0);
- //Switch
- switch(operation)
- {
- //Addition
- case '1':
- System.out.print("Please enter the first number: ");
- num1 = input.nextInt();
- System.out.print("Please enter the second number: ");
- num2 = input.nextInt();
- //Calculate and output
- ans = num1 + num2;
- System.out.printf(num1 + " + " + num2 + " = " + "%.2f", ans);
- System.out.print("\n");
- System.out.print("\n");
- break;
- //Subtraction
- case '2':
- System.out.print("Please enter the first number: ");
- num1 = input.nextInt();
- System.out.print("Please enter the second number: ");
- num2 = input.nextInt();
- //Calculate and output
- ans = num1 - num2;
- System.out.printf(num1 + " - " + num2 + " = " + "&.2f", ans);
- System.out.print("\n");
- System.out.print("\n");
- break;
- //Multiplication
- case '3':
- System.out.print("Please enter the first number: ");
- num1 = input.nextInt();
- System.out.print("Please enter the second number: ");
- num2 = input.nextInt();
- //Calculate and output
- ans = num1 * num2;
- System.out.printf(num1 + " * " + num2 + " = " + "%.2f", ans);
- System.out.print("\n");
- System.out.print("\n");
- break;
- //Division
- case '4':
- System.out.print("Please enter the first number: ");
- num1 = input.nextInt();
- System.out.print("Please enter the second number: ");
- num2 = input.nextInt();
- //Calculate and output
- ans = num1 / num2;
- System.out.printf(num1 + " / " + num2 + " = " + "&.2f", ans);
- System.out.print("\n");
- System.out.print("\n");
- break;
- //Exit
- case '5':
- System.exit(0);
- //Default
- default:
- System.out.print("Please check your selection.");
- }
- }
- while(operation != 5);
- }//End Main
- }//End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement