Advertisement
danisun18

Data Type

Jul 21st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DataType {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String word = scanner.nextLine();
  9.         String data = scanner.nextLine();
  10.  
  11.  
  12.         while ((!"".equals(word))&&(!"".equals(data))) {
  13.  
  14.  
  15.             System.out.println(isType(word, data));
  16.             word = scanner.nextLine();
  17.             data = scanner.nextLine();
  18.         }
  19.     }
  20.  
  21.     private static String isType(String word, String data) {
  22.  
  23.         String result = "";
  24.  
  25.         switch (word) {
  26.             case "int": {
  27.                 int res = 0;
  28.  
  29.                 int dataT = Integer.parseInt(data);
  30.                 res = dataT * 2;
  31.                 result = String.format("%d", res);
  32.             }
  33.             break;
  34.             case "real": {
  35.                 double res = 0;
  36.  
  37.                 double dataT = Double.parseDouble(data);
  38.                 res = dataT * 1.5;
  39.                 result = String.format("%.2f", res);
  40.  
  41.             }
  42.             break;
  43.             case "string": {
  44.               //  String result = "";
  45.  
  46.                 String dataT = data;
  47.                 result = String.format("$%s$", dataT);
  48.  
  49.             }
  50.             break;
  51.         }
  52.             return result;
  53.         }
  54.  
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement