Venciity

[SoftUni Java] Syntax - 06.FormattingNumbers

May 19th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class FormattingNumbers {
  6.    
  7.     public static void main(String[] args) {
  8.         Locale.setDefault(Locale.ROOT); // invariant culture
  9.         @SuppressWarnings("resource")
  10.         Scanner scanner = new Scanner(System.in);
  11.         int a = scanner.nextInt();
  12.        
  13.         if (a < 0 | a > 500) {
  14.             System.out.println("Please enter integer a (0 ≤ a ≤ 500)");
  15.             a = scanner.nextInt();
  16.         }
  17.        
  18.         String aHexString = Integer.toHexString(a).toUpperCase();
  19.         String aBinary = String.format("%10s", Integer.toBinaryString(a)).replace(' ', '0');
  20.         double b = scanner.nextDouble();
  21.         double c = scanner.nextDouble();
  22.        
  23.         if (c%1 == 0) {
  24.             System.out.printf("|%-10s|%s|%10.2f|%-10.0f|",aHexString,aBinary,b,c);
  25.         }
  26.         else {
  27.             System.out.printf("|%-10s|%s|%10.2f|%-10.3f|",aHexString,aBinary,b,c);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment