Java: Wieso funktioniert der Zähler vom Stack nicht?
Außer folgender Klasse gibt es noch eine Klasse Queue und Stack. Kann mir jemand erklären, warum die Münzen des 1 und 2 Euro sowie des 50 Cents Stapels funktionieren und die des 20 und 10 Cent Stapels nicht? Danke.
package muenzen;
import java.util.*;
public class muenzen {
public static int muenze;
public static int ausgabe, zaehler, zaehler2, zaehler3, zaehler4, zaehler5;
public static double wert2, wert;
static Scanner sc = new Scanner(System.in);
static Stack<Integer> muenzen = new Stack<>();
static Stack<Integer> zehncent = new Stack<>();
static Stack<Integer> zwanzigcent = new Stack<>();
static Stack<Integer> fuenfzigcent = new Stack<>();
static Stack<Integer> eineuro = new Stack<>();
static Stack<Integer> zweieuro = new Stack<>();
public static void stapelerstellen() {
for (int i = 0; i < 20; i++) {
System.out.println("Welchen Wert hat die Münze? (10 Cent/ 20 Cent/ 50 Cent/ 1 Euro/ 2 Euro)");
muenze = sc.nextInt();
if (muenze == 1) {
eineuro.push(muenze);
}
else if (muenze == 2) {
zweieuro.push(muenze);
}
else if (muenze == 10) {
zehncent.push(muenze);
}
else if (muenze == 20) {
zwanzigcent.push(muenze);
}
else if (muenze == 50) {
fuenfzigcent.push(muenze);
}
else {
System.out.println("Ungültige Eingabe. Bitte probieren Sie es erneut.");
}
}
}
public static void stapelausgabe() {
zaehler = 0;
zaehler2 = 0;
zaehler3 = 0;
zaehler4 = 0;
zaehler5 = 0;
while (!eineuro.isEmpty() || !zweieuro.isEmpty() || !zehncent.isEmpty() || !zwanzigcent.isEmpty() || !fuenfzigcent.isEmpty() ) {
while (!eineuro.isEmpty()) {
eineuro.pop();
zaehler = zaehler + 1;
}
wert = zaehler;
System.out.println("Auf dem 1€ Stapel liegen/liegt " + zaehler + " Münze(n). Er hat somit einen Wert von " + wert + "€");
while (!zweieuro.isEmpty()) {
zweieuro.pop();
zaehler2 = zaehler2 + 1;
}
wert = zaehler2 * 2;
System.out.println("Auf dem 2€ Stapel liegen/liegt " + zaehler2 + " Münze(n). Er hat somit einen Wert von " + wert + "€");
while (!fuenfzigcent.isEmpty()) {
fuenfzigcent.pop();
zaehler3 = zaehler3 + 1;
}
wert = zaehler3 * 50 / 100;
wert2 = wert * 100;
System.out.println("Auf dem 50 Cent Stapel liegen/liegt " + zaehler3 + " Münze(n). Er hat somit einen Wert von " + wert + "€ oder " + wert2 + " Cent");
while (!zwanzigcent.isEmpty()) {
zwanzigcent.pop();
zaehler4 = zaehler4 + 1;
}
wert = zaehler4 * 20 / 100;
wert2 = wert * 100;
System.out.println("Auf dem 20 Cent Stapel liegen/liegt " + zaehler4 + " Münze(n). Er hat somit einen Wert von " + wert + "€ oder " + wert2 + " Cent");
while (!zehncent.isEmpty()) {
zehncent.pop();
zaehler5 = zaehler5 + 1;
}
wert = zaehler5 * 10 / 100;
wert2 = wert * 100;
System.out.println("Auf dem 10 Cent Stapel liegen/liegt " + zaehler5 + " Münze(n). Er hat somit einen Wert von " + wert + "€ oder " + wert2 + " Cent");
}
}
public static void main(String[] args) {
stapelerstellen();
stapelausgabe();
}
}
I probably shouldn’t be raised here, should I? Otherwise you may have an empty stack at the end.
In an integer division, the next, absolutely smaller integer is rounded off. 50 by 100 is therefore 0.
So best to save the value in cents.