# JAVA die

• 10-24-2003, 10:50 AM
russman0316
JAVA die
Me and a friend have been working on this for a while, and we can't get it . The program supposed to roll the dice(twice) and compare both scores..

Here's what we came up with:
Code:

import javax.swing.*;
import java.util.*;

public class Dice {
private Random random = new Random();

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

public int[]tossResults(int number)
{
int[] result = {0,0,0,0,0,0,0,0,0,0,0,0,0};
for (int i=0; i<number; i++)
result[roll()]++;
return result;
}

public static void main(String [] args)
{

int userMoney;
int numberOfRolls;
int[] diceThrows;
//int myDie1 = regularDie.getroll();

String input = JOptionPane.showInputDialog
("Enter the initial amount of money in dollars (no coins allowed)");

userMoney = Integer.parseInt(input);
numberOfRolls=userMoney;

for(int i=1; i<=userMoney; i--)
{
Dice dice = new Dice();
diceThrows = dice.tossResults(numberOfRolls);
userMoney--;
}

if (die1==1 && die2==1)
{ userMoney++;
JOptionPane.showMessageDialog(null, "You hit snake eyes, you won \$1, and your value now is \$"+userMoney);

}

if(die1==2 && die2==2)
{
userMoney+=2;
JOptionPane.showMessageDialog(null, "You hit double two, you won \$2, and your value now is \$"+userMoney);
}

if(die1==3 && die2==3)
{ userMoney+=3;
JOptionPane.showMessageDialog(null, "You hit double three, you won \$3, and your value now is \$"+userMoney);
}

if(die1==4 && die2==4)
{ userMoney+=4;
JOptionPane.showMessageDialog(null, "You hit double four, you won \$4, and your value now is \$"+userMoney);
}

if(die1==5 && die2==5)
{ userMoney+=5;
JOptionPane.showMessageDialog(null, "You hit double five, you won \$5, and your value now is \$"+userMoney);
}

if(die1==6 && die2==6)
{ userMoney+=6;
JOptionPane.showMessageDialog(null, "You hit double six, you won \$6, and your value now is \$"+userMoney);
}

if (die2 < die1)

userMoney-=die2;

if (die2 > die1)

userMoney-=die1;

if (userMoney < 1)
{

JOptionPane.showMessageDialog(null, "You have lost all your money. Have a great day :)");
System.exit(0);
}

System.exit(0);
}
}

I don't think we need array for this, do we? and how the heck do you get die1 and die1 into the public static void main(String [] args)? And it doesn't even roll :/

• 10-25-2003, 07:49 PM
ArchAngel
This won't even compile!

It looks to me a classic case of continuing coding even if it doesn't work! You code seems quite confused and not very Object-Oriented.

Let me suggest this you write this program again, defining:
Code:

class Dice {
/**
* Rolls a single dice.
* @return A number between 1 and 6.
*/
public int roll();
}

...and then in your game code create TWO Dice objects (this is the joy of OO - you can just create another!).

Also, you scoring code could be much more efficient. Check first to see if the two dice values are equal i.e. result1 == result2 and then just add one of these values to the user's money i.e. money += result1.