catched input mismatch exceptions
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -34,7 +35,13 @@ class Fahrkartenautomat
|
||||
System.out.printf(" %s [%.2f EUR] (%d)\n", bezeichnungen[i], preise[i], i+1);
|
||||
System.out.printf(" Bezahlen (%d)\n", i+1);
|
||||
System.out.print("Ihre Wahl: ");
|
||||
try {
|
||||
auswahlTicket = tastatur.nextByte();
|
||||
}
|
||||
catch(InputMismatchException e) {
|
||||
auswahlTicket = 0;
|
||||
tastatur.nextLine();
|
||||
}
|
||||
try {
|
||||
einzelBetrag = preise[auswahlTicket-1];
|
||||
}
|
||||
@@ -45,7 +52,13 @@ class Fahrkartenautomat
|
||||
|
||||
while (true) {
|
||||
System.out.print("Anzahl der Tickets: ");
|
||||
try {
|
||||
anzahlTickets = tastatur.nextByte();
|
||||
}
|
||||
catch(InputMismatchException e) {
|
||||
anzahlTickets = 0;
|
||||
tastatur.nextLine();
|
||||
}
|
||||
if (!(anzahlTickets > 0 && anzahlTickets <= 10)) {
|
||||
System.out.print("Es wurde ein ungültiger Wert für die Ticketanzahl eingegeben. \n"
|
||||
+ "Bitte erneut versuchen.\n");
|
||||
@@ -77,7 +90,13 @@ class Fahrkartenautomat
|
||||
List<Double> muenzen = Arrays.asList(2.0, 1.0, 0.5, 0.2, 0.1, 0.05);
|
||||
while (true) {
|
||||
System.out.print("Eingabe (mind. 5Ct, höchstens 2 Euro): ");
|
||||
try {
|
||||
eingeworfeneMuenze = tastatur.nextDouble();
|
||||
}
|
||||
catch(InputMismatchException e) {
|
||||
eingeworfeneMuenze = 0;
|
||||
tastatur.nextLine();
|
||||
}
|
||||
//Muenze prüfen
|
||||
if (!muenzen.contains(eingeworfeneMuenze)) {
|
||||
System.out.println("Es wurde eine ungültige Münze eingeworfen! Bitte erneut versuchen");
|
||||
|
||||
Reference in New Issue
Block a user