Advertisement
JustACodingStudent

Two Value Calculator

Feb 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package calculator;
  2.  
  3. import java.util.Scanner;
  4. public class Calculator {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.         int x, y, ans, ans2;
  9.         char op, res;
  10. //     Loops the program in case the user wishes to calculate multiple problems
  11.         do
  12.         {
  13.          ans = 1;
  14.          ans2 = 1;
  15. //      Prints a list of supported operations  
  16.          System.out.println("-------------------------------");
  17.          System.out.println("List of operations:\n");
  18.          System.out.println("Addition: +\nSubtraction: -\nMultiplication: *\nDivision = /\nModulus: %\nFactorial: !");
  19.          System.out.println("-------------------------------\n");
  20. //      User input
  21.          System.out.print("Enter first value: ");
  22.          x = input.nextInt();
  23.          System.out.print("Enter second value: ");
  24.          y = input.nextInt();
  25.          System.out.print("Enter operation: ");
  26.          op = input.next().charAt(0);
  27. //      Checks to see if an inputted operation is incorrect
  28.          while((op != '+') && (op != '-') && (op != '*') && (op != '/') && (op != '%') && (op != '!'))
  29.          {
  30.              System.out.print("Unrecognized operation. Please re-enter: ");
  31.              op = input.next().charAt(0);
  32.          }
  33. //      Checks to see if the user is trying to divide by zero  
  34.          while((op == '/') && (y == 0))
  35.          {
  36.              System.out.print("Cannot divide by 0. Please re-input second value: ");
  37.              y = input.nextInt();
  38.          }
  39. //      Addition  
  40.          if(op == '+')
  41.          {
  42.              ans = x + y;
  43.          }
  44. //      Subtraction  
  45.          else if(op == '-')
  46.          {
  47.              ans = x - y;
  48.          }
  49. //      Multiplication  
  50.          else if(op == '*')
  51.          {
  52.              ans = x * y;
  53.          }
  54. //      Division  
  55.          else if(op == '/')
  56.          {
  57.              ans = x / y;
  58.          }
  59. //      Modulus Division  
  60.          else if(op == '%')
  61.          {
  62.              ans = x % y;
  63.          }
  64. //      Factorial  
  65.          else
  66.          {
  67.              for(int i = 1; i <= x; i++)
  68.              {
  69.                  ans = ans * i;
  70.              }
  71.              
  72.              for(int w = 1; w <= y; w++)
  73.              {
  74.                  ans2 = ans2 * w;
  75.              }
  76.              
  77.          }
  78. //      Prints answer for factorial  
  79.          if(op == '!')
  80.          {
  81.              System.out.println("The factorial of " + x + " is " + ans + ".");
  82.              System.out.println("The factorial of " + y + " is " + ans2 + ".");
  83.          }
  84. //      Prints answer if not a factorial  
  85.          else
  86.          {
  87.              System.out.println("The answer is " + ans + ".");
  88.          }
  89. //      Asks the user if they would like to reset the calculator and input another problem  
  90.          System.out.print("Would you like to reset? (y/n): ");
  91.          res = input.next().charAt(0);
  92. //      Checks for caps sensitive answers  
  93.          while((res == 'Y') || (res == 'N'))
  94.          {
  95.              System.out.print("This program is case sensitive. Please re-input in lowercase: ");
  96.              res = input.next().charAt(0);
  97.          }
  98. //      Checks for responses other than yes or no
  99.          while((res != 'y') && (res != 'n'))
  100.          {
  101.              System.out.print("Unrecognized response. Please re-enter: ");
  102.              res = input.next().charAt(0);
  103.          }
  104.         }while(res == 'y');
  105.     }
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement