Advertisement
Guest User

binary

a guest
Oct 18th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.  
  6.         Scanner in = new Scanner(System.in);
  7.         in.useLocale(Locale.US);
  8.         long number, choice, wahl;
  9.         String convert;
  10.         byte ByteSize = 8, ShortSize = 16, IntSize = 32, LongSize = 64, FloatSize = 32, DoubleSize = 64;
  11.  
  12.         System.out.println("Введите число для конвертации:");
  13.         number = in.nextLong();
  14.  
  15.         System.out.println("Выберите куда конвертировать \n  1 - двоичный формат числа \n  2 - Восьмиричная форма  \n  3 - Шеснадцатиричная форма");
  16.         choice = in.nextLong();
  17.  
  18.  
  19.         if (choice == 1) {
  20.              convert = Long.toBinaryString(number);
  21.             if (convert.length() < 8) {
  22.                 System.out.println("куда конвертировать? \n 1 - Byte \n 2 - Short \n 3 - Int \n 4 - Long \n 5 - Float \n 6 - Double ");
  23.                 wahl = in.nextLong();
  24.  
  25.                 if (wahl == 1) {
  26.                     for (int i= convert.length(); i != ByteSize; i++)
  27.                     { System.out.print("0"); } System.out.println(convert); }
  28.                 else if (wahl == 2) {
  29.                     for (int i= convert.length(); i != ShortSize; i++)
  30.                     { System.out.print("0"); } System.out.println(convert);  }
  31.                 else if (wahl == 3) {
  32.                     for (int i= convert.length(); i != IntSize; i++)
  33.                     { System.out.print("0"); } System.out.println(convert);  }
  34.                 else if (wahl == 4) {
  35.                     for (int i= convert.length(); i != LongSize; i++)
  36.                     { System.out.print("0"); } System.out.println(convert); }
  37.                 else if (wahl == 5) {
  38.                     for (int i= convert.length(); i != FloatSize; i++)
  39.                     { System.out.print("0"); }
  40.                     Float parse = Float.parseFloat(convert); System.out.println(parse); }
  41.                 else if (wahl == 6) {
  42.                     for (int i= convert.length(); i != DoubleSize; i++)
  43.                     { System.out.print("0"); } Double parse = Double.parseDouble(convert); System.out.println(parse); }
  44.                 else {System.out.println("Пожалуйста, введите корректные данные"); }
  45.                 return;             }
  46.  
  47.             if (convert.length() < 16) {
  48.                 System.out.println("куда конвертировать? \n 1 - Short \n 2 - Int \n 3 - Long \n 4 - Float \n 5 - Double ");
  49.                 wahl = in.nextLong();
  50.  
  51.                 if (wahl == 1) {
  52.                     for (int i= convert.length(); i != ShortSize; i++)
  53.                     { System.out.print("0"); } System.out.println(convert); }
  54.                 else if (wahl == 2) {
  55.                     for (int i= convert.length(); i != IntSize; i++)
  56.                     { System.out.print("0"); } System.out.println(convert);  }
  57.                 else if (wahl == 3) {
  58.                     for (int i= convert.length(); i != LongSize; i++)
  59.                     { System.out.print("0"); } System.out.println(convert); }
  60.                 else if (wahl == 4) {
  61. //                    for (int i= convert.length(); i != FloatSize; i++)
  62. //                    { System.out.print("0"); }
  63.                     Float parse = Float.parseFloat(convert); System.out.println(parse); }
  64.                 else if (wahl == 5) {
  65.                     for (int i= convert.length(); i != DoubleSize; i++)
  66.                     { System.out.print("0"); } Double parse = Double.parseDouble(convert); System.out.println(parse); }
  67.                 else {System.out.println("Пожалуйста, введите корректные данные"); }
  68.                 return; }
  69.  
  70.             if (convert.length() < 32) {
  71.                 System.out.println("куда конвертировать? \n  1 - Int \n  2 - Long \n  3 - Float \n  4 - Double ");
  72.                 wahl = in.nextLong();
  73.  
  74.                 if (wahl == 1) {
  75.                     for (int i= convert.length(); i != IntSize; i++)
  76.                     { System.out.print("0"); } System.out.println(convert);  }
  77.                 else if (wahl == 2) {
  78.                     for (int i= convert.length(); i != LongSize; i++)
  79.                     { System.out.print("0"); } System.out.println(convert); }
  80.                 else if (wahl == 3) {
  81.                     for (int i= convert.length(); i != FloatSize; i++)
  82.                     { System.out.print("0"); } Float parse = Float.parseFloat(convert); System.out.println(parse);  }
  83.                 else if (wahl == 4) {
  84.                     for (int i= convert.length(); i != DoubleSize; i++)
  85.                     { System.out.print("0"); } Double parse = Double.parseDouble(convert); System.out.println(parse); }
  86.                 else {System.out.println("Пожалуйста, введите корректные данные"); }
  87.                 return; }
  88.  
  89.             if (convert.length() < 64) {
  90.                 System.out.println("куда конвертировать?  \n 1 - Long  \n 2 - Double ");
  91.                 wahl = in.nextLong();
  92.  
  93.                 if (wahl == 1) {
  94.                     for (int i= convert.length(); i != LongSize; i++)
  95.                     { System.out.print("0"); } System.out.println(convert); }
  96.                 else if (wahl == 2) {
  97.                     for (int i= convert.length(); i != DoubleSize; i++)
  98.                     { System.out.print("0"); } Double parse = Double.parseDouble(convert); System.out.println(parse); }
  99.                 else {System.out.println("Пожалуйста, введите корректные данные"); }
  100.                 return; }
  101.  
  102.             convert = Long.toOctalString(number);
  103.             System.out.println(convert); }
  104.  
  105.         else if (choice == 2)
  106.         { convert = Long.toOctalString(number); System.out.println(convert); }
  107.         else if (choice == 3)
  108.         { convert = Long.toHexString(number); System.out.println(convert); }
  109.         else {System.out.println("Пожалуйста, введите корректные данные"); }
  110.      }
  111.     }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement