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 2 of 2

Thread: [Homework]Game of craps help

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Homework]Game of craps help

    I am currently working on a homework assignment and would like some guidance as to where to solve a part of a problem. I'm not looking for answers, just suggestions/guidance as to where to start from where I currently am in the program to solve what I need to do. I am making a game of craps(I have already solved the majority of the problem), and I need to have the output print 10 stars for every game they win, then print the total games won out of how many games played at the end. This is what the output should look like....

    You win
    **********
    Game 2, press enter to roll
    You rolled 1 1
    You lose
    **********
    Game 3, press enter to roll
    You rolled 5 6
    You win
    ********************
    You won 2 game(s) out of 3

    This is my code so far....which essentially just plays the game of craps without the output required above.

    import java.util.*;
    import java.math.*;


    public class GameOfCraps {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of games: ");
    int numGames = input.nextInt();
    while(numGames <= 0){
    System.out.print("Number must be > 0, please enter again: ");
    numGames = input.nextInt();
    }
    GameNumber(numGames);

    }

    // Method to run actual game of craps
    public static void CrapsGame(){
    int roll1, roll2, total;
    Random random = new Random();
    Scanner input = new Scanner(System.in);
    System.out.print("press enter to roll");
    input.nextLine();
    roll1 = 1 + random.nextInt(6);
    roll2 = 1 + random.nextInt(6);
    total = roll1 + roll2;
    if(total == 7 || total == 11){
    System.out.println("You rolled " + roll1 + " " + roll2 + "\nYou win");
    }
    else if(total == 2 || total == 3 || total == 12){
    System.out.println("You rolled " + roll1 + " " + roll2 + "\nYou lose");
    }
    else if(total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10){
    System.out.println("You rolled " + roll1 + " " + roll2);
    System.out.print("Point is " + total + " press enter to roll");
    input.nextLine();
    roll1 = 1 + random.nextInt(6);
    roll2 = 1 + random.nextInt(6);
    total = roll1 + roll2;
    if(total == 7){
    System.out.println("You rolled " + roll1 + " " + roll2 + "\nYou lose");
    }
    else{
    System.out.println("You rolled " + roll1 + " " + roll2 + "\nYou win");
    }
    }
    }

    // Method to generate number of games to play based on input
    public static void GameNumber(int num){
    for(int i = 1; i <= num; i++){
    System.out.print("Game " + i +", ");
    CrapsGame();
    }
    }

    }

    I'm not really sure where to approach this part of the program, I know I need maybe a counter variable with a loop to figure out how many games they have won so far...but I guess I'm just kind of lost as to which method to put it in...or if I should create a new method entirely....any suggestions would be appreciated thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: [Homework]Game of craps help

    Use a variable to keep track of how many games have been won. Write a method that uses a loop to print out N stars where N = 10 * number of wins.
    Last edited by Junky; November 7th, 2011 at 04:41 PM. Reason: Hangs head in shame
    Improving the world one idiot at a time!

Similar Threads

  1. Homework
    By jdonaldson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 11:09 AM
  2. Making a Craps game.
    By SOK in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2010, 08:23 PM
  3. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM
  4. Game of Craps: won't compile
    By FreeBird in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 15th, 2010, 05:12 PM
  5. Craps- GAME fine tunning.
    By Beaney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 3rd, 2010, 08:32 PM