Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Kalkulator {
- static Scanner userInput = new Scanner(System.in);
- public static void main(String[] arg) {
- System.out.print("Input your first number");
- int ena = userInput.nextInt();
- System.out.println("Input your second number");
- int dva = userInput.nextInt();
- System.out.println("What logical operation would you like to perform(E.g. + , - , / , *)?");
- String placeholder = userInput.next(); //Grabs the user's input in String
- char op = placeholder.charAt(0); //This grabs the first letter/character of the placeholder variable that I've assigned above
- /*\
- * I'll now perform all the logical operations before
- * So that I simply output them based
- * On the user's input
- */
- int totalSum = ena + dva;
- int totalSub = ena - dva;
- int totalDiv = ena / dva;
- int totalMul = ena * dva;
- if(op == '+'){
- System.out.print("Total sum = " + totalSum);
- }
- else if(op == '-'){
- System.out.print("Total subtraction = " + totalSub);
- }
- else if(op == '*'){
- System.out.print("Total multiplication = " + totalMul);
- }
- else if(op == '/'){
- System.out.print("Total division = " + totalDiv);
- }
- else{ System.out.println("You have inputted an invalid operator");}
- /*
- * The above code if what we call
- * Nested condition
- * This is basically checking one by one
- * Whether the user's operator input is equal to one of the above.
- * If you user hasn't inputted a correct input (E.g 'a')
- * The console will print out the error
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement