Results 1 to 8 of 8
- 12-04-2008, 09:39 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
New to Java, need help with program
Hello, I'm new to Java. I have been using Java for a couple of months. I need help with a program that uses dice to play a game of craps. The program needs to run 1000 games and I need to know:
1. how many games are won on the first, second,..., twentieth rolls and after the twentieth roll?
2.how many games are lost on the first, second,..., twentieth rolls and after the twentieth roll?
3. What are the chances of winning at craps?
4. What is the average length of a game of craps?
5. Do the chances of winning improve with the length of the game?
Here's my dice class:
class dice
{
int ar [] [] = new int [6] [6];
public dice()
{
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 6; j++)
{
ar [i] [j] = i+j+2;
}
}
}
public int get_dice(int rand1, int rand2)
{
return ar [rand1] [rand2];
}
}
Here's my random number generator:
import java.util.Random;
class my_random
{
Random X= new Random();
public my_random(long seed_value)
{
X.setSeed(seed_value);
}
public int get_num()
{
return(X.nextInt(6));
}
}
Heres my my driver for the main program: I know, its flawed:o
import java.util.Random;
class crapsdriver
{
public static void main(String args [])
{
dice classdice_x = new dice();
my_random rand_x = new my_random(56);
int dice_roll = 0;
int random_a = 0;
int random_b = 0;
int counter = 0;
for(int roll=1; roll <1000; roll++)
System.out.println("Number of games lost is:");
counter++;
System.out.println("Number of games won is:");
counter++;
random_a = rand_x.get_num();
random_b = rand_x.get_num();
dice_roll = classdice_x.get_dice(random_a, random_b);
if(dice_roll == 7 || dice_roll == 11)
System.out.println("You Win!");
if(dice_roll == 2 || dice_roll == 12)
System.out.println("You Lose");
while(true)
{
System.out.println("sum= " + dice);
System.out.println(random_a);
System.out.println(random_b);
if(dice_roll == dice_roll)
{
System.out.println("You Win!");
break;
}
if(dice_roll == 7)
{
System.out.println("You Lose");
break;
}
if(dice_roll != 7)
{
System.out.println("Roll Again");
break;
}
}
}
}
I will appreciate any and all help. Thank you.
- 12-04-2008, 10:54 PM #2
Member
- Join Date
- Dec 2008
- Posts
- 41
- Rep Power
- 0
you could use a for loop. that way u can control how many times it iterates. the code would look like this
for(int i=0;i<1000;1++){
the game code;
}
for displaying how many times it won you could use an accumulator value which could be
int wins=0//accumulator value
if(win==true)
win+=1;
then just display wins on the console.
-
1) what problems are you having now?
2) you may want to repost your code using code tags so that it will retain its formatting. To do this place the tag [code] above your code and the tag [/code] below the code.
- 12-04-2008, 11:19 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
I won't have access to a computer that has Java for a couple of days:( I'm just trying to figure out what's wrong. Thanks for the help!!
- 12-04-2008, 11:40 PM #5
Some observations
- This will only loop for 999 times, not 1000
Java Code:for(int roll=1; roll <1000; roll++)
Either make roll = 0 or roll< 1001 to get 1000 rolls- The following will ALWAYS result true:
Java Code:if(dice_roll == dice_roll) { System.out.println("You Win!"); break; }it should be dice_roll== (something else)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
Whose computer are you on right now?
The reason I ask is that it is somewhat futile to ask for help here if you can't use our recommendations to change your code and compile and test it. Without this ability you're just setting yourself up for a headache.
If you're at a home computer perhaps you can simply load the Java developers kit. It doesn't take up much space and is easy to use.
- 12-05-2008, 01:15 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 3
- Rep Power
- 0
Whose computer are you on right now?
I just bought a new computer and it doesn't have Java on it.
-
Similar Threads
-
Run program from Java
By nenadm in forum Advanced JavaReplies: 4Last Post: 12-07-2008, 10:36 PM -
Java program help
By Crystaliss in forum New To JavaReplies: 1Last Post: 11-20-2008, 03:46 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
New to Java Program
By jvasilj1 in forum New To JavaReplies: 1Last Post: 02-05-2008, 07:22 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks