RGAlex

O1. Data Types / Methods - More Exercise

Feb 9th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. package MethodsMoreExercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O1DataTypes {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String type = scanner.nextLine();
  9.         String input = scanner.nextLine();
  10.         printResult(type, input);
  11.     }
  12.  
  13.     static void printResult(String type, String input) {
  14.         switch (type) {
  15.             case "int":
  16.                 int number = Integer.parseInt(input);
  17.                 System.out.println(number * 2);
  18.                 break;
  19.             case "real":
  20.                 double realNumber = Double.parseDouble(input);
  21.                 double result = realNumber * 1.5;
  22.                 System.out.printf("%.2f", result);
  23.                 break;
  24.             default:
  25.                 System.out.printf("$%s$", input);
  26.         }
  27.  
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment