Advertisement
sanyakasarova

Operations Between Numbers

Sep 13th, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int numberOne = Integer.parseInt(scanner.nextLine());
  7.         int numberTwo = Integer.parseInt(scanner.nextLine());
  8.  
  9.         String operator = scanner.nextLine();
  10.  
  11.         int result = 0;
  12.  
  13.         switch (operator) {
  14.             case "+":
  15.                 result = numberOne + numberTwo;
  16.                 break;
  17.             case "-":
  18.                 result = numberOne - numberTwo;
  19.                 break;
  20.             case "/":
  21.                 result = numberOne / numberTwo;
  22.                 break;
  23.             case "*":
  24.                 result = numberOne * numberTwo;
  25.                 break;
  26.             case "%":
  27.                 result = numberOne % numberTwo;
  28.                 break;
  29.  
  30.         }
  31.  
  32.         // This is the way the code should be changed in order to work - notice that you had missed the spaces in the message:
  33.         // So instead of printing let's say 5+4=9, it should be 5 + 4 = 9
  34.         System.out.println(numberOne + " " + operator + " " + numberTwo + " = " + result);
  35.        
  36.         // Also, here's a better way to do the same thing, using placeholders:
  37.         // System.out.printf("%d %s %d = %d", numberOne,operator,numberTwo,result);
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement