Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Beschreibung
- *
- * @version 1.0 vom 01.12.2015
- * @author
- */
- import java.io.*;
- import java.io.*;
- public class UmwandlungBinärDeziHexa {
- public static String dezimalNachBinaer (int dezimal){
- String help = "";
- int a = dezimal;
- while (a > 0) {
- if (a % 2 == 0)
- help = help + "0";
- else
- help = help + "1";
- a /= 2;
- }
- String zurueck = help;
- return zurueck;
- }
- public static void main(String[] args) throws IOException {
- BufferedReader input = new BufferedReader(new InputStreamReader (System.in));
- String auswahlSt,eingabeZahlSt, ausgabe;
- int auswahlInt = 0,eingabeZahlInt = 0;
- boolean durchlass, durchlass2;
- System.out.println("--------------------------------");
- System.out.println(">> Umwandler <<");
- System.out.println("--------------------------------");
- System.out.println();
- do {
- System.out.println("->> Was möchten Sie umwandeln?");
- System.out.println("");
- System.out.println("->> Dezimal nach Binär (1)");
- System.out.println("->> Dezimal nach Hexadezimal (2)");
- System.out.println("->> Binär nach Dezimal (3)");
- System.out.println("->> Hexadezimal nach Binär (4)");
- System.out.println();
- System.out.print ("->> ");
- auswahlSt = input.readLine();
- durchlass = true;
- try {
- auswahlInt = Integer.parseInt(auswahlSt);
- if (auswahlInt < 1 || auswahlInt > 4) {
- System.out.println("Es nuss eine Zahl zwischen 1 und 4 sein.");
- durchlass = false;
- } // end of if
- } catch(Exception e) {
- System.out.println("Sie müssen eine gerade Zahl eingeben.");
- durchlass = false;
- }
- } while (durchlass != true);
- do {
- System.out.println();
- System.out.print("->> Bitte geben Sie ihre Zahl ein: ");
- eingabeZahlSt = input.readLine();
- durchlass2 = true;
- try {
- eingabeZahlInt = Integer.parseInt(eingabeZahlSt);
- if (eingabeZahlInt < 0) {
- System.out.println("Die Zahl muss positiv sein.");
- durchlass2 = false;
- } // end of if
- } catch(Exception e) {
- System.out.println("Sie müssen eine gerade Zahl eingeben.");
- durchlass2 = false;
- } // end of try
- } while (durchlass2 != true);
- if (auswahlInt == 1) {
- UmwandlungBinärDeziHexa.dezimalNachBinaer(eingabeZahlInt);
- ausgabe = zurueck;
- for (int i = ausgabe.length()-1; i>=0; i--) {
- System.out.print("->> " + ausgabe.charAt(i));
- }
- }
- } // end of main
- } // end of class UmwandlungBinärDeziHexa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement