|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
New To Java - Need some help
Hi,
I am very new to Java, and trying to solve a problem: A player rolls two dice.If the total is seven, the player wins otherwise the player loses. Need help in this. Thankyou |
|
#2
|
||||
|
||||
|
So, where's the problem?
Why not post some code and we can try to help you out...
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
|
#3
|
||||
|
||||
|
Code:
package ;
import java.awt.*;
public class {
public boolean roll() {
double dice1,dice2,ans=7;
dice1=Math.round(Math.random()*6); // a dice has six sides
dice2=Math.round(Math.random()*6); // a dice has six sides
dice1=dice1+dice2; //use one dice as a temp variable
if (dice1=ans) {return true;}
else {return false;}
}
public void init() {
if (roll()) {JOptionPane.showMessageDialog(null, "You rolled a seven!!");}
else {JOptionPane.showMessageDialog(null,"You did not roll a seven!!");}
}
}
just insert the package decloration and the class name above... hope this helped colton22 Last edited by colton22 : September 25th, 2006 at 04:46 PM. |
|
#4
|
|||
|
|||
|
Re:
Thanks for the code..
I tried this and seems to work fine: Code:
public class PairOfDice {
public static void main(String[] args) {
PairOfDice dice; // A variable that will refer to the dice.
int rollCount; // Number of times the dice have been rolled.
dice = new PairOfDice(); // Create the PairOfDice object.
rollCount = 0;
/* Roll the dice until they come up snake eyes. */
/* do {
dice.roll();
System.out.println("Dice1: " + dice.getDie1()
+ " Dice2: " + dice.getDie2());
rollCount++;
} while (dice.getTotal() != 7);
*/
System.out.println("Dice 1: " + dice.getDie1()
+ " and Dice 2: " + dice.getDie2());
/* Report the number of rolls. */
//System.out.println("\nIt took " + rollCount + " rolls to get a 2.");
if (dice.getTotal() == 7)
System.out.println("\nPlayer won!!!");
else
System.out.println("\nPlayer lost!!!");
}
private int die1; // Number showing on the first die.
private int die2; // Number showing on the second die.
public PairOfDice() {
// Constructor. Rolls the dice, so that they initially
// show some random values.
roll(); // Call the roll() method to roll the dice.
}
public void roll() {
// Roll the dice by setting each of the dice to be
// a random number between 1 and 6.
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
public int getDie1() {
// Return the number showing on the first die.
return die1;
}
public int getDie2() {
// Return the number showing on the second die.
return die2;
}
public int getTotal() {
// Return the total showing on the two dice.
return die1 + die2;
}
} // end class PairOfDice
/*
Rolls a pair of dice until the dice come up snake eyes
(with a total value of 2). Counts and reports the
number of rolls.
*/
Do you know how I can use the above code to follow this question: roll a pair of dice 1000 times, counting the number of box cars (two sixes) that occurs. The below doesnt seem to work..Can you pls help me..in modifying whats required. Code:
public class PairOf1000Dice
{
public static void main (String[]args){
private final int MAX = 6; // maximum face value
private int FV1; // current value showing on die 1
private int FV2; //current value showing on die 2
public PairOf1000Dice(){
{
FV1 = 0;
FV2 = 0;
}
public int roll_1()
{
FV1 = (int)(Math.random() * MAX) + 1;
return FV1;
}
public int roll_2()
{
FV2 = (int)(Math.random() * MAX) + 1;
return FV2;
}
public void setFaceValue1 (int value1)
{
FV1 = value1;
}
public void setFaceValue2(int value2)
{
FV2 = value2;
}
public int getFaceValue1()
{
return FV1;
}
public int getFaceValue2()
{
return FV2;
}
public String toString()
{
String result = Integer.toString(FV1+FV2);
return result;
}
}
}
}
Thanks Last edited by MadCowDzz : September 26th, 2006 at 09:03 AM. Reason: Added [code] tags |
|
#5
|
|||
|
|||
|
Re: Error with the following code
Hi:
When I roll a pair of dice 1000 times, counting the number of box cars (two sixes) that occurs. Heres a code for it but I get an error saying syntax error on token public , "new" expected. Can you please help in correcting what might have been wrong here. Thank you Heres the code: Code:
/*
* Created on Sep 24, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
*
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PairOf1000Dice
{
public static void main (String[]args){
private final int MAX = 6; // maximum face value
private int FV1; // current value showing on die 1
private int FV2; //current value showing on die 2
public PairOf1000Dice(){
{
FV1 = 0;
FV2 = 0;
}
public int roll_1()
{
FV1 = (int)(Math.random() * MAX) + 1;
return FV1;
}
public int roll_2()
{
FV2 = (int)(Math.random() * MAX) + 1;
return FV2;
}
public void setFaceValue1 (int value1)
{
FV1 = value1;
}
public void setFaceValue2(int value2)
{
FV2 = value2;
}
public int getFaceValue1()
{
return FV1;
}
public int getFaceValue2()
{
return FV2;
}
public String toString()
{
String result = Integer.toString(FV1+FV2);
return result;
}
}
}
}
Last edited by MadCowDzz : September 26th, 2006 at 09:04 AM. Reason: Added [code] tags |
|
#6
|
||||
|
||||
|
Please wrap your code in [code][/code] when pasting code into the forums...
It makes the code easier to read. ![]() |
|
#7
|
|||
|
|||
|
Doing this in a graphical surrounding is a tad hard if you're very new, so I'll limit it to console output.
Get a Main class and a Dice class. The Dice class has the (possibly static) int returning method named throw(), which uses the Math.random() method to simulate a dice throw. When you launch Main, it'll create a new instance of Dice (Dice d = new Dice()). Then it'll execute Dice's throw command 1000 times (Dice.throw();), with a for-loop and counts the number of times a 6 has been thrown with the help of an internal counter. In the end, it outputs it to the console (System.out.print();). If you need more help, go and post. As for your current code: It's probabely expecting something akin to: Pair p = new PairOf1000Dice. |
|
#8
|
|||
|
|||
|
i think hes a skiddie free-loading.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > New To Java - Need some help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|