Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /**
  2. *
  3. * Beschreibung
  4. *
  5. * @version 1.0 vom 01.12.2015
  6. * @author
  7. */
  8.  
  9.  
  10. import java.io.*;
  11. import java.io.*;
  12. public class UmwandlungBinärDeziHexa {
  13.  
  14. public static String dezimalNachBinaer (int dezimal){
  15.  
  16. String help = "";
  17. int a = dezimal;
  18.  
  19. while (a > 0) {
  20. if (a % 2 == 0)
  21. help = help + "0";
  22.  
  23. else
  24. help = help + "1";
  25. a /= 2;
  26.  
  27. }
  28.  
  29. String zurueck = help;
  30.  
  31. return zurueck;
  32. }
  33.  
  34.  
  35.  
  36.  
  37. public static void main(String[] args) throws IOException {
  38.  
  39. BufferedReader input = new BufferedReader(new InputStreamReader (System.in));
  40. String auswahlSt,eingabeZahlSt, ausgabe;
  41. int auswahlInt = 0,eingabeZahlInt = 0;
  42. boolean durchlass, durchlass2;
  43.  
  44.  
  45.  
  46. System.out.println("--------------------------------");
  47. System.out.println(">> Umwandler <<");
  48. System.out.println("--------------------------------");
  49. System.out.println();
  50.  
  51.  
  52.  
  53. do {
  54.  
  55. System.out.println("->> Was möchten Sie umwandeln?");
  56. System.out.println("");
  57. System.out.println("->> Dezimal nach Binär (1)");
  58. System.out.println("->> Dezimal nach Hexadezimal (2)");
  59. System.out.println("->> Binär nach Dezimal (3)");
  60. System.out.println("->> Hexadezimal nach Binär (4)");
  61. System.out.println();
  62. System.out.print ("->> ");
  63. auswahlSt = input.readLine();
  64.  
  65. durchlass = true;
  66.  
  67. try {
  68. auswahlInt = Integer.parseInt(auswahlSt);
  69. if (auswahlInt < 1 || auswahlInt > 4) {
  70. System.out.println("Es nuss eine Zahl zwischen 1 und 4 sein.");
  71. durchlass = false;
  72. } // end of if
  73. } catch(Exception e) {
  74. System.out.println("Sie müssen eine gerade Zahl eingeben.");
  75. durchlass = false;
  76. }
  77. } while (durchlass != true);
  78.  
  79. do {
  80. System.out.println();
  81. System.out.print("->> Bitte geben Sie ihre Zahl ein: ");
  82. eingabeZahlSt = input.readLine();
  83.  
  84. durchlass2 = true;
  85.  
  86. try {
  87. eingabeZahlInt = Integer.parseInt(eingabeZahlSt);
  88. if (eingabeZahlInt < 0) {
  89. System.out.println("Die Zahl muss positiv sein.");
  90. durchlass2 = false;
  91. } // end of if
  92. } catch(Exception e) {
  93. System.out.println("Sie müssen eine gerade Zahl eingeben.");
  94. durchlass2 = false;
  95. } // end of try
  96.  
  97.  
  98. } while (durchlass2 != true);
  99.  
  100.  
  101. if (auswahlInt == 1) {
  102. UmwandlungBinärDeziHexa.dezimalNachBinaer(eingabeZahlInt);
  103. ausgabe = zurueck;
  104. for (int i = ausgabe.length()-1; i>=0; i--) {
  105. System.out.print("->> " + ausgabe.charAt(i));
  106. }
  107. }
  108.  
  109. } // end of main
  110.  
  111. } // end of class UmwandlungBinärDeziHexa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement