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 8815285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:20:15+00:00 2026-06-14T04:20:15+00:00

In my assignment i have to make a simple version of Craps, for some

  • 0

In my assignment i have to make a simple version of Craps, for some reason the percentage assignments always produce 0 even when both variables are non 0, here is the code.

import java.util.Random;

Header, note the variables

public class Craps {
private int die1, die2,myRoll ,myBet,point,myWins,myLosses;
private double winPercent,lossPercent;
private Random r = new Random();

Just rolls two dies and produces their some.

public int roll(){
    die1 = r.nextInt(6)+1;
    die2 = r.nextInt(6)+1;
    return(die1 + die2);
}

The Play method, this just loops through the game.

public void play(){
    myRoll = roll();
    point = 0;

    if(myRoll == 2 ||myRoll == 3 || myRoll == 12){
        System.out.println("You lose!");
        myLosses++;
    }else if(myRoll == 7 || myRoll == 11){
        System.out.println("You win!");
        myWins++;
    }else{
        point = myRoll;
        do {
            myRoll = roll();
        }while(myRoll != 7 && myRoll != point);
        if(myRoll == point){
            System.out.println("You win!");
            myWins++;
        }else{
            System.out.println("You lose!");
            myLosses++;
        }
    }
}

This is where the bug is, this is the tester method.

public void tester(int howMany){
    int i = 0;
    while(i < howMany){
        play();
        i++;
    }

bug is right here in these assignments statements

    winPercent = myWins/i * 100;
    lossPercent = myLosses/i* 100;
    System.out.println("program ran "+i+" times "+winPercent+"% wins "+ lossPercent+"% losses with "+myWins+" wins and "+myLosses+" losses");



}

}

  • 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-14T04:20:17+00:00Added an answer on June 14, 2026 at 4:20 am

    Probably because you are doing an integer division, and denominator is greater than numerator. So the value will be zero.

    You can change this assignment: –

    winPercent = myWins/i * 100;
    lossPercent = myLosses/i* 100;
    

    to: –

    winPercent = myWins * 100.0/i;
    lossPercent = myLosses * 100.0/i;
    

    There are two things to consider: –

    • Either make your division a floating point of division, by casting
      your numerator to float: –

      winPercent = (float)(myWins)/i * 100;
      
    • Also, in case of integer division, its the matter of associativity
      and precendence. Since * and / have same precendence, and
      have left-to-right associativity. So, myWins / i will be
      evaluated first. So, there is bright chances that that value can be
      0.

      So, just change the order of * and / to make the * happen
      first.

    Note, I have used 100.0 in multiplication. Because multiplying by 100 will again result in Truncation.

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

Sidebar

Related Questions

I have an assignment and I need to make my own (simple) generic linked
For a programming assignment, I have to make a simple dice game in Visual
I have to make a simple animation for an assignment and don't have access
I have a homework assignment to make a simple program in assembly language for
I have an assignment in my Operating Systems class to make a simple pseudo-stack
For an assignment in college, we have to make a script in Perl that
I am quite new to SignalR. My first assignment is to make simple chat
Alright, so for a homework assignment I had to make a simple application with
I'm stuck with a c++ assignment where I should make a simple thread and
This shouldn't be this hard, but I'm stuck. We have a simple assignment where

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.