Results 1 to 4 of 4
Thread: Exceptions?
- 03-07-2011, 01:24 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Exceptions?
I'm writing a small craps game in java. I need help writing exceptions int his code.
Edit:Java Code:package craps; import java.util.Scanner; import java.util.Random; class Craps { public static void main(String[] args) { //welcome message. Prompts the user to: System.out.println("Welcome!"); System.out.println("Choose your difficulty:"); System.out.println("Press 1 for easy (500 dollars)"); System.out.println("Press 2 for intermediate (300 dollars)"); System.out.println("Press 3 for expert (100 dollars)"); int difficultyc = difficulty_checker(); //prints the amount of money user has once difficulty has been selected System.out.println("money = " + difficultyc); System.out.println("Bet some money!"); int bet = bet_checker(difficultyc); //prints the amount of money user has after user has bet some System.out.println("You now have: " + bet + " dollars."); int craps = game_craps(bet, difficultyc); //prints the amount of money the user has after winning/losing System.out.println("You now have: " + craps + " dollars."); } /** * Determines what difficulty user will use */ public static int difficulty_checker(){ Scanner myScanner = new Scanner(System.in); int difficulty = myScanner.nextInt(); int money = 0; switch(difficulty){ case 1: money = 500; break; case 2: money = 300; break; case 3: money = 100; break; case 4: money = 1000; System.out.println("Aha! secret difficulty: moron."); break; // i need an exception here if the user doesn't input a number between 1 - 4 } return money; } /** * Determines how much money user will bet */ public static int bet_checker(int money){ Scanner myScanner = new Scanner(System.in); int better = myScanner.nextInt(); money -= better; return money; //need an exception here if user bets more money than user has } /** * Executing the game craps */ public static int game_craps(int money, int better){ Random myRandom = new Random(); Scanner myScanner = new Scanner(System.in); System.out.println("Press 1 to play!"); int play = myScanner.nextInt(); if(play == 1){ //need an exception here if user doesn't press 1 int die1 = myRandom.nextInt(6) + 1; int die2 = myRandom.nextInt(6) + 1; System.out.print("die1: " + die1 + " "); System.out.println("die2: " + die2); if(die1 + die2 == 4 | die1 + die2 == 7){ better *= 2; money += better; System.out.println("You won!"); } else { System.out.println("You lose!"); } } else { System.out.println("What?"); } return money; } }
Also! I need help with writing a line of code: after the program executes craps, it just stops. I want the user to be able to bet again. How would I do that?
edit again...:
i also need an exception in case the user inputs a decimal number, as obviously all the numbers are ints.Last edited by Eranga; 03-07-2011 at 08:02 AM. Reason: code tags added
- 03-07-2011, 05:27 AM #2
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
*bump* help, please?
- 03-07-2011, 06:56 AM #3
Member
- Join Date
- Aug 2007
- Posts
- 26
- Rep Power
- 0
For the switch case,
you can use the 'default' case. In case user does not enter between 1-4, it will automatically go to default wherein you can throw an exception and catch it in your main.
For all the other places where you require a exception, you can call throws new Exception() and then catch it in your main program and print the appropriate message.
Hope this helps!
Thanks,
- 03-07-2011, 08:03 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Explaining Exceptions
By sunde887 in forum New To JavaReplies: 5Last Post: 01-22-2011, 08:00 AM -
Exceptions
By Nerijus in forum New To JavaReplies: 8Last Post: 05-18-2010, 01:44 PM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks