Results 1 to 4 of 4

Thread: Exceptions?

  1. #1
    linc186 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Exceptions?

    I'm writing a small craps game in java. I need help writing exceptions int his code.
    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;
    	}
    
    }
    Edit:
    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

  2. #2
    linc186 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    *bump* help, please?

  3. #3
    revathi17 is offline Member
    Join Date
    Aug 2007
    Posts
    26
    Rep Power
    0

    Default

    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,

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by linc186 View Post
    Edit:
    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.
    So what you've tried so far?

Similar Threads

  1. Explaining Exceptions
    By sunde887 in forum New To Java
    Replies: 5
    Last Post: 01-22-2011, 08:00 AM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 01:44 PM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •