Question
Dear, experts!, I got help so far but still I have one more problem. Because of the last fix my programm is now fully capable
Dear, experts!, I got help so far but still I have one more problem. Because of the last fix my programm is now fully capable to solve the merrySeven problem. But can someone fix my code for the last try and catch statement? Its working when the player is putting "Y" or "N" but for all other letters or characters like cyc<142 the program stops. Instead of this it should ask the Player again. Do you want to continue ? (Y) I would appreciate a quick help for this. Greetings ! import java.io.BufferedReader; import java.io.InputStreamReader;
public class Aufgabe03_1 {
final static int[] TABLE1 = { 2, 4, 9, 6, 11 }; final static int[] TABLE2 = { 3, 5, 8, 10, 12 };
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int updateBalance = 0; int win = 0; int bet = 0; int guess = 0; String input = new String(); System.out.println("Hallo bei Lustige Sieben."); System.out.println("====================================== "); do { // start of do while loop System.out.println("Bitte Deinen Einsatz zwischen 1 und 100: ");
while (true) { try { input = br.readLine(); bet = Integer.parseInt(input);
if (bet < 1 || bet > 100) { throw new Exception(); }
} catch (Exception e) { System.out.println("Falscher Einsatz! Bitte Deinen Einsatz zwischen 1 und 100: "); continue; } break; } System.out.println(String.format("Du setzt %d, damit ist Dein Kontostand = %d", bet, updateBalance - bet )); System.out.println(" Bitte Deine Zahl zwischen 2 und 12: ");
while (true) { try { input = br.readLine(); guess = Integer.parseInt(input);
if (guess < 2 || guess > 12) { throw new Exception(); }
} catch (Exception e) { System.out.println("Falsche Zahl! Bitte Deine Zahl zwischen 2 und 12: "); continue; } break; } // Roll the dice int dice1 = (int) (Math.random() * 6) + 1; int dice2 = (int) (Math.random() * 6) + 1; int dicethrow = dice1 + dice2; System.out.format("Die Wrfel sind gefallen: %d + %d = %d ", dice1, dice2, dicethrow); //--------------------------------------------------------------------------------------------------------------// // Possibilities for updating the balance based on dicethrow and bet, is shown now below // //--------------------------------------------------------------------------------------------------------------// // 1) If dicethrow is 2,4,6,9,11 --> Search in the left table side if the guess number is on the left table as well. if ( dicethrow == 2 || dicethrow == 4 || dicethrow == 6 || dicethrow == 9 || dicethrow == 11 ) { for ( int i = 0; i < TABLE1.length; i++ ) { if ( guess == TABLE1[i] ) { win = bet; // If the guess number is on the left side the player will get the bet back. } else if ( guess == TABLE2[i] ) { win = -bet; // If not the guess number is on the right table side so the player will loose the bet. } } } // 2) If dicethrow is 7 and guess = 6 for an example, the player is supposed to loose the bet only for the right Table if ( dicethrow == 3 || dicethrow == 5 || dicethrow == 7 || dicethrow == 8 || dicethrow == 10 || dicethrow == 12 ) { for ( int i = 0; i < TABLE1.length; i++ ) { if ( guess == TABLE1[i] ) { win = -bet; } else if ( guess == 7 ) { win = -bet; // For handeling the situation guess == 7 and dicethrow == 3 using an extra else if statement. Loosing the bet! } } } // 3) If dicethrow is 3,5,8,10,12 --> Search in the right table side if the guess number is on the right table as well. if ( dicethrow == 3 || dicethrow == 5 || dicethrow == 8 || dicethrow == 10 || dicethrow == 12 ) { for ( int i = 0; i < TABLE2.length; i++ ) { if ( guess == TABLE2[i] ) { win = bet; // If the guess number is on the right side the player will get the bet back. } else if ( guess == TABLE1[i] ) { win = -bet; // If not the guess number is on the left table side so the player will loose the bet. } } } // 4) If dicethrow is 7 and guess = 5, the player is supposed to loose the bet only for the left Table if ( dicethrow == 2 || dicethrow == 4 || dicethrow == 6 || dicethrow == 7 || dicethrow == 9 || dicethrow == 11 ) { for ( int i = 0; i < TABLE2.length; i++ ) { if ( guess == TABLE2[i] ) { win = -bet; } else if ( guess == 7 ) { win = -bet; // For handeling the situation guess == 7 and dicethrow == 2 using an extra else if statement. Loosing the bet! } } } // 5) Special dice Situation where the user can get twice or thrice times the bet back */ if ( guess == dicethrow ) { if ( guess == 7 ) { win = 3 * bet; // Refait --> win thrice } else { win = 2 * bet; // dicethrow is equal to guess number --> win twice } } //--------------------------------------------------------------------------------------------------------------// // Finished the complete Dice logic for merry seven // //--------------------------------------------------------------------------------------------------------------// // Print all Solutions and furthermore check with try and catch if the player wants to continue whether or not if (win > 0) { updateBalance = updateBalance - bet + win; System.out.format("Du bekommst zurck: %d ", win); } if (win < 0) { updateBalance = updateBalance + win; System.out.format("Du hast Deinen Einsatz von %d verloren ", -win); } System.out.format("Dein neuer Kontostand = %d ", updateBalance ); System.out.print("Willst du aufhren (Y)? "); try { input = br.readLine().toUpperCase(); } catch (Exception e) { throw new Exception(); }
if (input.equals("N")) { System.out.println("--------------------------------------");
} else if (input.equals("Y") || input.isEmpty()) { System.out.println("--------------------------------------"); System.out.format("Dein Endstand = %d ", updateBalance); System.out.println("======================================"); } } while (input.equals("N")); // check for condition after first iteration } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started