Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Hi Lo Dice Betting

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Hi Lo Dice Betting

    Hello guys..

    I want to write a small Hi Lo Betting Dice program that satisfies these conditions :

    A player places a bet on whether the sum of two dice will come up High (totalling 8 or higher), Low (totalling 6 or less) or Sevens (totalling exactly 7). If the player wins, he receives a payout based on the schedule given in the table below:


    Choice Payout
    High 1 x Wager
    Low 1 x Wager
    Sevens 4 x Wager

    The player will start with $100 for wagering. If the player chooses to wager $0 or if he runs out of money, the program should end. Otherwise it should ask him whether he's wagering on High, Low or Sevens, display the results of the die rolls, and update his money total accordingly.

    some sample output:
    You have 100 dollars.
    Enter an amount to bet (0 to quit): 50
    High, low or sevens (H/L/S): H
    Die 1 rolls: 1
    Die 2 rolls: 5
    Total of two dice is: 6
    You lost!

    You have 50 dollars.
    Enter an amount to bet (0 to quit): 25
    High, low or sevens (H/L/S): L
    Die 1 rolls: 6
    Die 2 rolls: 2
    Total of two dice is: 8
    You lost!

    You have 25 dollars.
    Enter an amount to bet (0 to quit): 12
    High, low or sevens (H/L/S): H
    Die 1 rolls: 2
    Die 2 rolls: 6
    Total of two dice is: 8
    You won 12 dollars!


    I have written some code but now i am stuck at the logic maybe and i don't know how should i go ahead with it.. I don't want the exact codes but i want direction as to how should i go about doing my business.. Any help will be highly appreciated..

    (I am just starting at Java and i found this exercise online and so i want to try to do it myself)

    package com.peg.hilodice;
    import java.util.Scanner;
     
     
    public class DiceGame {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		int money = 100;
    		int bet;
    		System.out.println("Hello! Welcome to Hi-Lo Dice Betting");
    		System.out.println("You have $" + money + "." );
     
    		System.out.println("How much $ would you like to bet ?");
    		Scanner sc = new Scanner(System.in);
    		bet = sc.nextInt();
     
    		if (bet > money ) {
     
    			System.out.println("You don't have enough funds");
     
    		}else if (bet < money) {
     
    			System.out.println("Choose your guess: H for Hi, L for Lo and S for Seven");
    			Scanner scr = new Scanner(System.in);
    			String guess = scr.nextLine();
    			System.out.println("You chose : " + guess);
    			int diceOne = (int)(Math.random()*6) + 1;
    			System.out.println("Dice one is : " + diceOne);
    		    int diceTwo = (int)(Math.random()*6) + 1;
    		    System.out.println("Dice two is : " + diceTwo);
    			int diceTotal = diceOne + diceTwo;
    			System.out.println("Your Total : " + diceTotal);
     
    			if (diceTotal > 7) {
     
    				System.out.println("It's a HI");
     
    			}else if (diceTotal <7) {
     
    				System.out.println("It's a LO");
     
    			}else  {
     
    				System.out.println("It's SEVEN");
    			}
     
     
     
    		}
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Lo Dice Betting

    Its better to work out the logic before trying to write any code. Make a list of the steps and the tests the code needs to make.

    What steps have you worked out so far?
    Where are you having problems?

    Once the logic is found, then try coding it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    smartshahezad (June 30th, 2014)

  4. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Hi Lo Dice Betting

    else if (bet < money)
    That will produce a logical error if the user entered the exact starting amount.

    I scanned over the code and it seems o.k to me. Try testing it out - did it do what you expected?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    smartshahezad (June 30th, 2014)

  6. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by Norm View Post
    Its better to work out the logic before trying to write any code. Make a list of the steps and the tests the code needs to make.

    What steps have you worked out so far?
    Where are you having problems?

    Once the logic is found, then try coding it.
    Quote Originally Posted by Ada Lovelace View Post
    That will produce a logical error if the user entered the exact starting amount.

    I scanned over the code and it seems o.k to me. Try testing it out - did it do what you expected?

    Wishes Ada xx

    So i managed to figure out the methods in my class.. Now, i want to write the main method.. And i have no clue how and what to write there..

    Below is my code :

     
    package com.peg.hilodice;
     
    import java.util.Scanner;
     
    public class DiceGame {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    	}
     
    	private static int getBet() {
     
    		System.out.println("Hello! Welcome to Dice Hi-Lo");
    		System.out.println("You have $100 !");
    		System.out.println("How much $ do you want to bet ?");
     
    		System.out.println("Input 0 to end");
     
    		Scanner inScanner = new Scanner(System.in);
    		int a = inScanner.nextInt();
    		inScanner.close();
     
    		return a;
     
    	}
     
    	private static char getHighLow() {
     
    		System.out.println("What do you want to bet ?");
    		System.out.println("Type H for Hi, L for Lo and S for Seven :");
     
    		Scanner inScanner = new Scanner(System.in);
    		char ch = inScanner.next().trim().charAt(0);
    		char chr = Character.toUpperCase(ch);
    		inScanner.close();
     
    		return chr;
     
    	}
     
    	private static int getRoll() {
     
    		int a = (int) (Math.random() * 6) + 1;
    		return a;
     
    	}
     
    	private static int winAmount() {
     
    		int rollOne = getRoll();
    		int rollTwo = getRoll();
    		char chr = getHighLow();
    		int betAmount = getBet();
    		int winAmount=0;
    		char HighLow;
     
    		int roll = rollOne + rollTwo;
     
    		if (roll > 7) {
     
    			System.out.println("It is a Hi");
    			HighLow = 'H';
     
    		} else if (roll < 7) {
     
    			System.out.println("It is a Lo");
    			HighLow = 'L';
     
    		} else {
     
    			System.out.println("It is a SEVEN");
    			HighLow = 'S';
     
    		}
     
    		if (chr == HighLow) {
     
    			switch (chr) {
     
    			case 'H':
     
    				winAmount = betAmount * 2;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			case 'L':
     
    				winAmount = betAmount * 2;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			case 'S':
     
    				winAmount = betAmount * 4;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			default:
     
    				System.out.println("No amount");
     
    			}
     
    		} else {
     
    			winAmount = 0;
    			System.out.println("You Lost !");
     
    		}
     
    		return winAmount;
     
    	}
     
    }

  7. #5
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Hi Lo Dice Betting

    You are about 70% through. You just need to add a few outputs to your winAmount() method.

    You need to greet the user in main and tell them they have $100.
    Then you get the bet and store it in some variable i.e. bet. This will cause bet to prompt the user for how much the want to bet. (The greeting messages should be in the main method - not in the bet method)

    Do you now have an idea of what you are supposed to do, specifically in the winAmount() method to display the remaining information to the user?
    Last edited by pyler; July 1st, 2014 at 03:01 AM. Reason: Misread post

  8. The Following User Says Thank You to pyler For This Useful Post:

    smartshahezad (July 1st, 2014)

  9. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Hi Lo Dice Betting

    Why are all your methods static? (main execluded). If they are only known to one
    class make them private, or public if they are going to be excuted within the main
    method (so it can "see" them.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  10. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    smartshahezad (July 1st, 2014)

  11. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Lo Dice Betting

    i want to write the main method.. And i have no clue how
    What is the main() method supposed to do? You can't write the code until you know what it is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    smartshahezad (July 1st, 2014)

  13. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by pyler View Post
    You are about 70% through. You just need to add a few outputs to your winAmount() method.

    You need to greet the user in main and tell them they have $100.
    Then you get the bet and store it in some variable i.e. bet. This will cause bet to prompt the user for how much the want to bet. (The greeting messages should be in the main method - not in the bet method)

    Do you now have an idea of what you are supposed to do, specifically in the winAmount() method to display the remaining information to the user?
    if i add more outputs in the winAmount() method, will it return more than one values ???

  14. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Lo Dice Betting

    will it return more than one values
    A method can only return one value each time it is called.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #10
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    this is what i want this program to do.. but repetitively.. i have managed to do it without it being repeated

    package com.peg.trn;
     
    import java.util.Scanner;
     
    public class HiLo {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		int winAmount;
    		int credit = 100;
     
    		System.out.println("Hello! Welcome to Hi-Lo");
    		System.out.println("How much do you want to bet ?");
    		System.out.println("Input 0 to end");
     
    		Scanner sc = new Scanner(System.in);
    		int betAmount = sc.nextInt();
     
    		if (betAmount == 0) {
     
    			System.out.println("Thanks for playing.. Your credit is : $"
    					+ credit);
     
    		} else if (betAmount > 100) {
     
    			System.out
    					.println("The bet amount should be less than 100.. Try again !");
     
    		} else {
     
    			System.out.println("What do you want to bet on ?");
    			System.out.println("Select H for Hi, L for Low and S for Seven");
     
    			char ch = sc.next().trim().charAt(0);
    			char chr = Character.toUpperCase(ch);
    			sc.close();
     
    			int rollOne = (int) (Math.random() * 6) + 1;
    			System.out.println("Dice one : " + rollOne);
     
    			int rollTwo = (int) (Math.random() * 6) + 1;
    			System.out.println("Dice 2 : " + rollTwo);
     
    			int sum = rollOne + rollTwo;
    			System.out.println("The sum of the two dice : " + sum);
     
    			char finalCh;
     
    			if (sum > 7) {
     
    				System.out.println("It is a Hi");
    				finalCh = 'H';
     
    			} else if (sum < 7) {
     
    				System.out.println("It is a Lo");
    				finalCh = 'L';
     
    			} else {
     
    				System.out.println("It is a Seven");
    				finalCh = 'S';
     
    			}
     
    			if (chr == finalCh) {
     
    				switch (chr) {
     
    				case 'H':
     
    					winAmount = betAmount * 2;
    					credit = credit + winAmount;
    					System.out.println("You win : " + winAmount);
    					System.out.println("Your new credit is $" + credit);
    					break;
     
    				case 'L':
     
    					winAmount = betAmount * 2;
    					credit = credit + winAmount;
    					System.out.println("You win : " + winAmount);
    					System.out.println("Your new credit is $" + credit);
    					break;
     
    				case 'S':
     
    					winAmount = betAmount * 4;
    					credit = credit + winAmount;
    					System.out.println("You win : " + winAmount);
    					System.out.println("Your new credit is $" + credit);
    					break;
     
    				default:
     
    					System.out.println("No amount");
     
    				}
     
    			} else {
    				credit = credit - betAmount;
    				System.out.println("You Lost !");
    				System.out.println("Your new credit is $" + credit);
     
    			}
    		}
     
    	}
     
    }

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Lo Dice Betting

    this is what i want this program to do.. but repetitively
    repetitively means using a loop like a while loop that continues looping until some event like a user's input tells the program to exit the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    smartshahezad (July 1st, 2014)

  18. #12
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by Norm View Post
    repetitively means using a loop like a while loop that continues looping until some event like a user's input tells the program to exit the loop.
    i know i have to write a while loop.. but i dont know how to write it in my scenario.. i can write a basic while loop that prints maybe 10 numbers.. but i dont know how to implement it in my case.. sorry but i am just starting out in this.. thanks

  19. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Hi Lo Dice Betting

    What needs to go inside the loop?
    What condition or event will end the looping?
    Either the while's condition or the break statement can be use to end the loop.

    Write a small practice program to experiment with to see how to use the while statement before trying to add it to your code.
    and before writing any code, write some pseudo code for the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    smartshahezad (July 1st, 2014)

  21. #14
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by Norm View Post
    What needs to go inside the loop?
    What condition or event will end the looping?
    Either the while's condition or the break statement can be use to end the loop.

    Write a small practice program to experiment with to see how to use the while statement before trying to add it to your code.
    and before writing any code, write some pseudo code for the loop.
    can you please tell me what's wrong with this ???


     
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		System.out.println("Hello! Welcome to Dice Hi-Lo");
    		System.out.println("You have $100 !");
     
    		int credit = 100;
     
    		while (credit > 0) {
     
    			int bet = getBet();
    			credit = credit - bet;
     
    			if (bet != 0) {
     
    				int winnings = winAmount();
    				credit = credit + winnings;
    				System.out.println("You now have $" + credit);
     
    			}else {
     
    				System.out.println("Thank you for playing.. Your balance is $" + credit);
    				break;
     
    			}
     
    		}
     
    	}


    --- Update ---

    Almost got it.. there is only one problem now.. below is my complete program..

     
    package com.peg.hilodice;
     
    import java.util.Scanner;
     
    public class DiceGame {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		System.out.println("Hello! Welcome to Dice Hi-Lo");
    		System.out.println("You have $100 !");
     
    		int credit = 100;
     
    		while (credit > 0) {
     
    			int bet = getBet();
    			int winnings = winAmount();
    			credit = credit - bet;
     
    			if (bet != 0) {
     
    				credit = credit + winnings;
    				System.out.println("You now have $" + credit);
     
    			} else {
     
    				System.out.println("Thank you for playing.. Your balance is $"
    						+ credit);
    				break;
     
    			}
     
    			System.out.println("Thank you for playing");
    			System.out.println("Better luck next time");
    		}
     
    	}
     
    	private static int getBet() {
     
    		System.out.println("How much $ do you want to bet ?");
     
    		System.out.println("Input 0 to end");
     
    		Scanner inScanner = new Scanner(System.in);
    		int a = inScanner.nextInt();
    		// inScanner.close();
     
    		return a;
     
    	}
     
    	private static char getHighLow() {
     
    		System.out.println("What do you want to bet ?");
    		System.out.println("Type H for Hi, L for Lo and S for Seven :");
     
    		Scanner inScanner = new Scanner(System.in);
    		char ch = inScanner.next().trim().charAt(0);
    		char chr = Character.toUpperCase(ch);
    		// inScanner.close();
     
    		return chr;
     
    	}
     
    	private static int getRoll() {
     
    		int a = (int) (Math.random() * 6) + 1;
    		return a;
     
    	}
     
    	private static int winAmount() {
     
    		int rollOne = getRoll();
    		int rollTwo = getRoll();
    		int betAmount = getBet();
    		char chr = getHighLow();
    		int winAmount = 0;
    		char HighLow;
     
    		int roll = rollOne + rollTwo;
     
    		if (roll > 7) {
     
    			System.out.println("It is a Hi");
    			HighLow = 'H';
     
    		} else if (roll < 7) {
     
    			System.out.println("It is a Lo");
    			HighLow = 'L';
     
    		} else {
     
    			System.out.println("It is a SEVEN");
    			HighLow = 'S';
     
    		}
     
    		if (chr == HighLow) {
     
    			switch (chr) {
     
    			case 'H':
     
    				winAmount = betAmount * 2;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			case 'L':
     
    				winAmount = betAmount * 2;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			case 'S':
     
    				winAmount = betAmount * 4;
    				System.out.println("You win : " + winAmount);
    				break;
     
    			default:
     
    				System.out.println("No amount");
     
    			}
     
    		} else {
     
    			winAmount = 0;
    			System.out.println("You Lost !");
     
    		}
     
    		return winAmount;
     
    	}
     
    }

    in the main method, when the line int bet = getBet() is executed, it invokes the getBet() method.. now because my winAmount method also invokes the getBet method, when i run the program, it asks me to input my bet twice.. is there any way around this?? maybe where i can get the return value from the getBet method in my main class, without invoking the method twice???

    really appreciate the time of you guys

    thanks

  22. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Hi Lo Dice Betting

    Pass the value needed by the winAmount() method as a parameter and remove the code from winAmount() that calls getBet().

  23. The Following User Says Thank You to GregBrannon For This Useful Post:

    smartshahezad (July 2nd, 2014)

  24. #16
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by GregBrannon View Post
    Pass the value needed by the winAmount() method as a parameter and remove the code from winAmount() that calls getBet().
    sorry but can you give me an example please?? thank

  25. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Hi Lo Dice Betting

    It would look something like this:

    In your main() method:
    while (credit > 0) {
     
        int bet = getBet();
     
        // pass the parameter bet to the winAmount() method:
        int winnings = winAmount( bet );
     
        credit = credit - bet;
    Which then requires a change to the winAmount() method signature:

    private static int winAmount( int bet )

    And then . . .

    int betAmount = bet;

    At least one of your variables then become redundant, but you can discover that and clean it up as you do.

    Edit: I suspect using methods is new to you. When you get the hang of passing variables to them as I've demonstrated above, then you should discover efficiencies that will reduce the number of variables you have throughout your program.

  26. The Following User Says Thank You to GregBrannon For This Useful Post:

    smartshahezad (July 2nd, 2014)

  27. #18
    Junior Member
    Join Date
    Jan 2010
    Posts
    14
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Hi Lo Dice Betting

    Quote Originally Posted by GregBrannon View Post
    It would look something like this:

    In your main() method:
    while (credit > 0) {
     
        int bet = getBet();
     
        // pass the parameter bet to the winAmount() method:
        int winnings = winAmount( bet );
     
        credit = credit - bet;
    Which then requires a change to the winAmount() method signature:

    private static int winAmount( int bet )

    And then . . .

    int betAmount = bet;

    At least one of your variables then become redundant, but you can discover that and clean it up as you do.

    Edit: I suspect using methods is new to you. When you get the hang of passing variables to them as I've demonstrated above, then you should discover efficiencies that will reduce the number of variables you have throughout your program.
    THIS IS BRILLIANT.. thank you soooooooooooo much... now its working.. i am planning to add couple other things i learn in this program and keep updating this game i guess.. and yes i dont have the hang of it yet, and that's why i am trying to write as many programs as possible to eventually be good at it..

Similar Threads

  1. Dice Game! PLEASE HELP!
    By mhaxton in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2013, 12:47 PM
  2. [SOLVED] Dice Roller
    By astraya008 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2012, 06:40 PM
  3. [SOLVED] Dice game need help
    By lf2killer in forum Loops & Control Statements
    Replies: 6
    Last Post: October 5th, 2012, 02:25 AM
  4. Football Betting System
    By johndingli in forum Object Oriented Programming
    Replies: 5
    Last Post: June 29th, 2011, 08:44 AM
  5. Dice Program Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 2nd, 2010, 07:50 AM