Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:10:30+00:00 2026-06-14T11:10:30+00:00

I am teaching myself Java, and I created a program to simulate a game

  • 0

I am teaching myself Java, and I created a program to simulate a game of craps. I have the entire game in a Boolean method. Everything works fine, but I want to count every time the user wins. What is the best way to count these wins, and then print them?

Here is my code:

import java.util.Random;

public class game{

public static void main(String[] args) {
    Random rand = new Random();
    for(int i = 0; i <= 10; i++){
        craps(rand);
    }
}

public static boolean craps(Random randomGen){
    int roll1 = randomGen.nextInt(6) + 1;
    int roll2 = randomGen.nextInt(6) + 1;

    int sum = roll1 + roll2;
    int sumRepeat = sum;
    int count = 0;


    String win = "you win";
    String lose = "you lose";
    String point = "point=";

    if(sum == 7 || sum == 11){
        System.out.printf("[%d,%d] %d %s\n",roll1,roll2,sum,win);
        count++;
        return true;
    }
    else if(sum == 2 || sum == 3 || sum == 12){
        System.out.printf("[%d,%d] %d %s\n",roll1,roll2,sum,lose);
        return false;
    }
    else{
        System.out.printf("[%d,%d] %s %d",roll1,roll2,point,sum);
        int roll3 = randomGen.nextInt(6) + 1;
        int roll4 = randomGen.nextInt(6) + 1;
        int sum2 = roll3 + roll4;
        do{
            roll3 = randomGen.nextInt(6) + 1;
            roll4 = randomGen.nextInt(6) + 1;
            sum2 = roll3 + roll4;
            if(sum2 == sumRepeat){
                System.out.printf("[%d,%d] %d %s\n",roll3,roll4,sum2,win);
                count++;
                return true;
            }else{
                System.out.printf("[%d,%d]",roll3,roll4);
            }
        }
        while(sum2 != 7);{
            System.out.printf("[%d,%d] %d %s\n",roll3,roll4,sum2,lose);
            return false;
        }
    }
}
}

I have tried to return the count variable but I get an error saying I can’t return an int value from a Boolean method which I expected. But I also tried to print out the count value with System.out.println(); But I can’t find a place in the code to put it since I always get an error stating that the println is unreachable code.

Any help would be appreciated. Thanks!

Here is the output of the program

[1,6] 7 you win

[2,4] point= 6[3,6][3,3] 6 you win

[3,3] point= 6[5,1] 6 you win

[6,1] 7 you win

[6,3] point= 9[5,1][3,5][2,6][6,4][5,6][1,5][6,3] 9 you win

[3,3] point= 6[1,5] 6 you win

[5,1] point= 6[1,5] 6 you win

[2,2] point= 4[6,4][3,3][3,4][3,4] 7 you lose

[4,5] point= 9[6,4][5,1][1,2][6,2][1,5][3,5][6,5][1,5][5,2][5,2] 7 you lose

[2,2] point= 4[5,3][5,6][6,4][3,2][6,5][3,6][4,4][4,2][4,3][4,3] 7 you lose

[6,6] 12 you lose

You won 11 time(s).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:10:32+00:00Added an answer on June 14, 2026 at 11:10 am

    Count them outside the craps function, here’s one simple way:

    public static void main(String[] args) {
        Random rand = new Random();
        int counter = 0;
        for(int i = 0; i <= 10; i++){
            if(craps(rand)) counter++;
        }
    
        System.out.println("You won " + counter + " times!");
    }
    

    By the way, it’s important to note that it’s far superior to count outside the craps function than it is to modify it. As you learn java, remember that each method should only do one job. You may want to practice breaking the craps function into smaller pieces to practice this philosophy. The one method to one job philosophy will make larger projects much easier to understand and debug when you are looking back later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbie in java but I think I have done well teaching myself
I'm extremely new to Java, and have mostly just been teaching myself as I
I am teaching myself Java and I have a simple package with three classes
Im teaching myself java and the book i'm looking at just got around to
I'm teaching myself Python 3.2 and I'm trying to make a program to match
I have recently started teaching myself C# and Asp.net. I am trying to build
I'm teaching myself programming and today's challenge is to write a program that can
I am teaching myself to use JavaCC in a hobby project, and have a
i am teaching myself java and i work through the exercises in Thinking in
Im still teaching myself Java so I wanted to try to read a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.