Hey, ich möchte eine vier Bit Zahl hochzählen lassen und es dann als Binärzahl ausgeben. In der ersten for Schleife ist Dezimal in binär und die zweite for Schleife stellt die Zahlen übergroß dar.
Da Java eine Klassenorientierte Programmiersprache ist möchte ich als nächsten Schritt beide for Schleifen in Klassen unterteilen.
Ich hab schon ein bisschen rumprobiert die ganzen if-else abfragen in der ersten for schleife in eine andere Klasse zu packen, leider kamen immer falsche ausgaben als Ergebnis
Wie geh ich da am besten vor?
[CODE lang="java" title="Code"]public class Ctrl_count_up_binary {
public static int vierBit;
public static int length;
public static int count;
public static int countZwei;
public static String bitZahl;
public static String ergebnis;
public static String ziffer;
public static int ersterBit = 1;
public static int zweiterBit = 2;
public static int dritterBit = 4;
public static int vierterBit = 8;
public static String ersteStelle = "0";
public static String zweiteStelle = "0";
public static String dritteStelle = "0";
public static String vierteStelle = "0";
public static void main(String[] args) {
for (count = 0; count <= 15; count++) { //Dezimal in Binär
vierBit = count;
if (vierBit >= vierterBit) {
ersteStelle = "1";
vierBit = vierBit - vierterBit;
} else {
ersteStelle = "0";
}
if (vierBit >= dritterBit) {
zweiteStelle = "1";
vierBit = vierBit - dritterBit;
} else {
zweiteStelle = "0";
}
if (vierBit >= zweiterBit) {
dritteStelle = "1";
vierBit = vierBit - zweiterBit;
} else {
dritteStelle = "0";
}
if (vierBit >= ersterBit) {
vierteStelle = "1";
} else {
vierteStelle = "0";
}
bitZahl = ersteStelle + zweiteStelle + dritteStelle + vierteStelle;
System.out.println("Die Bitzahl von "+count+" ist: "+bitZahl);
for (countZwei = 0; countZwei < bitZahl.length(); countZwei++) { //Übergroße Zahlen
ziffer=bitZahl.charAt(countZwei)+"";
if (ziffer.equals("1")) {
System.out.println(" 1");
System.out.println("11");
System.out.println(" 1");
System.out.println(" 1");
System.out.println();
} else {
System.out.println(" 0 ");
System.out.println("0 0");
System.out.println("0 0");
System.out.println(" 0 ");
System.out.println();
}
}
}
}
}[/CODE]
Da Java eine Klassenorientierte Programmiersprache ist möchte ich als nächsten Schritt beide for Schleifen in Klassen unterteilen.
Ich hab schon ein bisschen rumprobiert die ganzen if-else abfragen in der ersten for schleife in eine andere Klasse zu packen, leider kamen immer falsche ausgaben als Ergebnis
Wie geh ich da am besten vor?
[CODE lang="java" title="Code"]public class Ctrl_count_up_binary {
public static int vierBit;
public static int length;
public static int count;
public static int countZwei;
public static String bitZahl;
public static String ergebnis;
public static String ziffer;
public static int ersterBit = 1;
public static int zweiterBit = 2;
public static int dritterBit = 4;
public static int vierterBit = 8;
public static String ersteStelle = "0";
public static String zweiteStelle = "0";
public static String dritteStelle = "0";
public static String vierteStelle = "0";
public static void main(String[] args) {
for (count = 0; count <= 15; count++) { //Dezimal in Binär
vierBit = count;
if (vierBit >= vierterBit) {
ersteStelle = "1";
vierBit = vierBit - vierterBit;
} else {
ersteStelle = "0";
}
if (vierBit >= dritterBit) {
zweiteStelle = "1";
vierBit = vierBit - dritterBit;
} else {
zweiteStelle = "0";
}
if (vierBit >= zweiterBit) {
dritteStelle = "1";
vierBit = vierBit - zweiterBit;
} else {
dritteStelle = "0";
}
if (vierBit >= ersterBit) {
vierteStelle = "1";
} else {
vierteStelle = "0";
}
bitZahl = ersteStelle + zweiteStelle + dritteStelle + vierteStelle;
System.out.println("Die Bitzahl von "+count+" ist: "+bitZahl);
for (countZwei = 0; countZwei < bitZahl.length(); countZwei++) { //Übergroße Zahlen
ziffer=bitZahl.charAt(countZwei)+"";
if (ziffer.equals("1")) {
System.out.println(" 1");
System.out.println("11");
System.out.println(" 1");
System.out.println(" 1");
System.out.println();
} else {
System.out.println(" 0 ");
System.out.println("0 0");
System.out.println("0 0");
System.out.println(" 0 ");
System.out.println();
}
}
}
}
}[/CODE]