Hi,

The following code is from a textbook. I cannot get it to work. Would someone be so kind as to look at the code as I am coming up with two errors. They are the two lines towards the bottom that read:

int die1 = 1 +  randomNumbers.nextInt(6);
int die2 = 1 + randomNumbers.next(6);

I assumed that because this code was in a school textbook that it would be fine but that is not happening. It shows a successful build but when I try to run it it says no main class found. I add public static void main etc and then there are errors all over the place.

I thought perhaps someone's keen eye might spot that without having to go through all the errors when I add main. I am very, very new to Java and find the language twisted and verbose. C++ was much easier for me. Any ideas? (I no longer have this class but wanted to see what a craps program looked like.lol)

import java.util.Random;

/**
 *
 * @author Lynn
 */
public class Craps
{
private enum Status { CONTINUE, WON, LOST };

 // create random number generator for use in method rollDice

private final static int SNAKE_EYES = 2;
private final static int TREY = 3;
private final static int SEVEN = 7;
private final static int YO_LEVEN = 11;
private final static int BOX_CARS = 12;



public void play()
{int myPoint = 0;
 Status gameStatus;
 int sumOfDice = rollDice();

 switch(sumOfDice){
        case SEVEN: // win with 7 on first roll
        case YO_LEVEN: // win with 11 on first roll
        gameStatus = Status.WON;
        case SNAKE_EYES: // lose with 2 on first roll
        case TREY: // lose with 3 on first roll
        case BOX_CARS: // lose with 12 on first roll
        gameStatus=Status.LOST;
break;
default:
        gameStatus = Status.CONTINUE;
        myPoint = sumOfDice;
        System.out.printf("Point is %d\n", myPoint);
break;
     }
while (gameStatus==Status.CONTINUE)
{
    sumOfDice = rollDice();
if (sumOfDice==myPoint)
gameStatus = Status.WON;
else
     if (sumOfDice==SEVEN) 
                gameStatus = Status.LOST;

}
if (gameStatus==Status.WON)
System.out.println("Player wins");
else
 System.out.println("Player loses");
}

public int rollDice()
{

int die1 = 1 +  randomNumbers.nextInt(6);
int die2 = 1 + randomNumbers.next(6);

int sum = die1 + die2;

System.out.printf("Player rolled %d + %d = %d\n", die1,die2,sum);
return sum;
}

}

Recommended Answers

All 6 Replies

Can you write your code please between "

" here your code "

" brackets?
The brackets without the quotations. This makes your problem easier to read.

ups the brackets again "[" code "]" here your code "[" /code "]" like i said, the brackets without quotations :)

ups the brackets again "[" code "]" here your code "[" /code "]" like i said, the brackets without quotations :)

You can use the noparse tag( [noparse][noparse][/noparse][/noparse] ) to make it more clear.

Writing:
[noparse]

[code]

// Your code here

[/code] [/noparse]

Will yield:

[code]

// Your code here

[/code]

Add the following line: private static final Random randomNumbers = new Random(); under:
// create random number generator for use in method rollDice int die2 = 1 + randomNumbers.next(6); must change to: int die2 = 1 + randomNumbers.next[B]Int[/B](6); Then run using the following class:

public class CrapsTest 
{
   public static void main( String[] args )
   {
      Craps game = new Craps();
      game.play(); // play one game of craps
   } // end main
} // end class CrapsTest

BTW, instead of manually keying in all the code examples from your book, you could as well download them from the website accompanying your book. This code is guaranteed to run. And if it doesn't, then you know that there's something wrong with your configuration.
Here's the download link for your book's code examples:
http://media.pearsoncmg.com/ph/esm/deitel/javahtp_8/code_examples_8e.html

Hi,

I appreciate the tip and the website. Thank you so much.

lynnajoe

Hi,

I appreciate your input but I have been running programs with Netbeans for months. There is nothing wrong with my configuration but when I went to that website and downloaded those codes, nothing was there. So perhaps there is something wrong with my configurations or could you try one (chapter 1) and let me know if it was empty or not. That being said then I have no idea what could be wrong with the configuration as it has been working for months.

Oh, and I thought I posted the code correctly within the code brackets so now I am really confused. I have posted here before and it was fine. Now I don't know what the problem is with my post. I don't even know what quotation marks? I posted it within the code as I have several times previously.

I did try the code that tux for life suggested and that did not work either.Could someone look at the entire code and see if I am missing an import or something please?

Lynnajoe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.