Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Kalkulator {
  4.     static Scanner userInput = new Scanner(System.in);
  5.     public static void main(String[] arg) {
  6.  
  7.         System.out.print("Input your first number");
  8.         int ena = userInput.nextInt();
  9.         System.out.println("Input your second number");
  10.         int dva = userInput.nextInt();
  11.         System.out.println("What logical operation would you like to perform(E.g. + ,  - , / , *)?");
  12.         String placeholder = userInput.next(); //Grabs the user's input in String
  13.         char op = placeholder.charAt(0); //This grabs the first letter/character of the placeholder variable that I've assigned above
  14.  
  15.         /*\
  16.          * I'll now perform all the logical operations before
  17.          * So that I simply output them based
  18.          * On the user's input
  19.          */
  20.         int totalSum = ena + dva;
  21.         int totalSub = ena - dva;
  22.         int totalDiv = ena / dva;
  23.         int totalMul = ena * dva;
  24.  
  25.         if(op == '+'){
  26.         System.out.print("Total sum = " + totalSum);
  27.         }
  28.         else if(op == '-'){
  29.         System.out.print("Total subtraction = " + totalSub);
  30.         }
  31.         else if(op == '*'){
  32.         System.out.print("Total multiplication = " + totalMul);
  33.         }
  34.         else if(op == '/'){
  35.         System.out.print("Total division = " + totalDiv);
  36.         }
  37.         else{ System.out.println("You have inputted an invalid operator");}
  38.         /*
  39.          * The above code if what we call
  40.          * Nested condition
  41.          * This is basically checking one by one
  42.          * Whether the user's operator input is equal to one of the above.
  43.          * If you user hasn't inputted a correct input (E.g 'a')
  44.          * The console will print out the error
  45.          */
  46.    }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement