By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

Craps wins and losses

P: 10
Hi, I'm writing a program that simulates 100 rounds of craps. I have the code written mostly. My one problem is that I can't seem to get the total numbers of wins and losses.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Random;
  2.  
  3. public class Test {
  4.  
  5.     /**
  6.      * This Program show the results of simulated games of craps using if else statements
  7.      */
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         Random rand=new Random();
  11.         int xWon=0;
  12.         int xLost=0;
  13.         for(int i=1;i<=100;i++){
  14.             craps(rand);
  15.             if(true){
  16.                 xWon++;
  17.             }else{
  18.                 xLost++;
  19.             }
  20.         }System.out.println("Games Won: "+xWon);
  21.          System.out.println("Games Lost: "+xLost);
  22.     }    
  23.     public static boolean craps(Random rand){
  24.         int dieI=rand.nextInt(6)+1;
  25.         int dieII=rand.nextInt(6+1);
  26.         int sumDies=dieI+dieII;
  27.             System.out.print("["+dieI+","+dieII+"]");
  28.  
  29.             if (sumDies==7||sumDies==11){
  30.                 System.out.println(sumDies+" You Win!!!");
  31.                 return true;
  32.             } else if(sumDies==2||sumDies==3||sumDies==12){
  33.                 System.out.println(sumDies+" You Lose...");
  34.                 return false;
  35.         }
  36.         int point=sumDies;
  37.                 System.out.print("Point= "+point+" ");
  38.  
  39.         do {
  40.             dieI=rand.nextInt(6)+1;
  41.             dieII=rand.nextInt(6+1);
  42.             sumDies=dieI+dieII;
  43.                 System.out.print("["+dieI+","+dieII+"]");
  44.         } while(sumDies!=7&&sumDies!=point);
  45.  
  46.         if (sumDies==point){
  47.             System.out.println(sumDies+" You Win!!!");
  48.             return true;
  49.         } else{
  50.             System.out.println(sumDies+" You Lose...");
  51.             return false;
  52.         }
  53.  
  54.  
  55.     }
  56.  
  57. }
  58.  
If someone could help me out that would be grand. I've been at this one part for 8 hours straight...
Mar 6 '12 #1

✓ answered by Rabbit

When you're checking whether or not it was a win, you have to actually check the return value.
Expand|Select|Wrap|Line Numbers
  1. if(craps(rand))

Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,422
When you're checking whether or not it was a win, you have to actually check the return value.
Expand|Select|Wrap|Line Numbers
  1. if(craps(rand))
Mar 6 '12 #2

P: 10
Thank-you so much! I literally tried everything except that.
Mar 6 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.