Hello - I'm a brand new student of programming and I'm needing help. I'm stonewalled. I've got an assignment that requires me to create a class named Dice that another program will use to roll dice.
The instructions are:

  • The class must be named Dice
  • A method named diceRoll() must be included
  • A random number between 1 and 6 inclusive must be generated
  • The random number value generated must be returned
  • The face of the die must be displayed below: (description of display not included here - I've got that in my code, and it works right)

Here is my code thus far. It compiles/runs fine - I think it does what it's supposed to do, BUT... I don't know how to create or implement or include this method diceRoll(), which professor's program needs to make hers work. This part confuses me. I take this course online - the book is helpful, but I have had little or no communication or help otherwise. Thanks in advance for any ideas:

import java.util.Random; // Needed for random dice numbers.

public class Dice
{
  public static void main (String[] args)
  { 
    int face; // The face of the die.


    // A random object for the program.
   Random randomNumbers = new Random();
   // Die face number is set equal to a random number.
   face = randomNumbers.nextInt(6)+ 1;

   // Display results.
   if (face == 1)
    System.out.println("One:\n\n *");
    else if (face == 2)
    System.out.println("Two:\n*\n\n  *");
    else if (face == 3)
    System.out.println("Three:\n*\n  *\n    *");
    else if (face == 4)
    System.out.println("Four:\n*  *\n\n*  *");
    else if (face == 5)
    System.out.println("Five:\n*   *\n  *\n*   *");
    else if (face == 6)
    System.out.println("Six:\n*  *\n*  *\n*  *");

    } 
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The method Math.random() returns a random double between 0 and 1.
0 < Math.Random <1;
So one variation of jwenting's post :

private int diceRoll() {
  double r=Math.Random();
  //do stuff
  return /* An [U]int[/U] number between 1 and 6 */
}

Thank you - this is so helpful. But unfortunately I still get errors when I put this in. This is what I added (in red), but I got the errors that you see below:
code:

private int diceRoll() { 	
  	// A random object for the program.
   Random randomNumbers = new Random();
   // Die face number is set equal to a random number.
   face = randomNumbers.nextInt(6)+ 1;

   // Display results.
   if (face == 1)
	System.out.println("One:\n\n *");
	else if (face == 2)
	System.out.println("Two:\n*\n\n  *");
	else if (face == 3)
	System.out.println("Three:\n*\n  *\n    *");
	else if (face == 4)
	System.out.println("Four:\n*  *\n\n*  *");
	else if (face == 5)
	System.out.println("Five:\n*   *\n  *\n*   *");
	else if (face == 6)
	System.out.println("Six:\n*  *\n*  *\n*  *");
 	[B]} [/B]   
	} 
}

And here are the 2 errors:
Dice.java:10: illegal start of expression
private int diceRoll() {
^
Dice.java:10: ';' expected
private int diceRoll() {
I keep getting something like this when I try to add this method.

Looks like you tried to add this function inside of your main function. You want it OUTSIDE of main, like this:

public class Dice
{
    private int diceRoll() 
    {
      double r=Math.Random();
      //do stuff
      return /* An int number between 1 and 6 */
    } 

    public static void main (String[] args)
    {	
      // main function contents
      // call diceRoll from here, but don't put the function in here.
    }
}

Thank you - this is so helpful. But unfortunately I still get errors when I put this in. This is what I added (in red), but I got the errors that you see below:
code:

private int diceRoll() { 	
  	// A random object for the program.
   Random randomNumbers = new Random();
   // Die face number is set equal to a random number.
   face = randomNumbers.nextInt(6)+ 1;

   // Display results.
   if (face == 1)
	System.out.println("One:\n\n *");
	else if (face == 2)
	System.out.println("Two:\n*\n\n  *");
	else if (face == 3)
	System.out.println("Three:\n*\n  *\n    *");
	else if (face == 4)
	System.out.println("Four:\n*  *\n\n*  *");
	else if (face == 5)
	System.out.println("Five:\n*   *\n  *\n*   *");
	else if (face == 6)
	System.out.println("Six:\n*  *\n*  *\n*  *");
 	[B]} [/B]   
	} 
}

And here are the 2 errors:
Dice.java:10: illegal start of expression
private int diceRoll() {
^
Dice.java:10: ';' expected
private int diceRoll() {
I keep getting something like this when I try to add this method.

Why can I not seem to leave the main string in:

public static void main (String[] args)

and also put the method in that I need?:

private int diceRoll()

I'm supposed to be building a class, so is it true that I don't need the public static void main....? If I take it out, at least my code compiles with the method in there, but when I try to run it, it says "NoSuchMethodError:main"
Somebody please help me! I'm really trying.

Well I'd say you should first test to make sure that your java JDK and JRE are installed right and that you can run/compile a simple program. Here's a Hello World program. Save it under filename "HelloWorld.java".

// HelloWorld.java
public class HelloWorld 
{
  public static void main( String[] args) 
  {
     System.out.println( "Hello World!" );
  }
}

This should compile and run without errors. It should simply display "Hello World!" to the console window. If you get either a compiler or runtime error, something other than your code is the problem. If it does work as it should, please repost your entire program. I'm not sure where you are trying to place everything.

I did the HelloWorld. It did fine.
I tried your suggestion. It seemed right to me, but I'm still doing something(s) wrong. Is it because I'm not generating the random numbers the way you are? But also, when I run this code, I am still getting the same error: java.lang.NoSuchMethodError: main
Exception in thread "main"

Plus, should I get rid of the whole "face" variable? I have to display each of the six faces of the die in a certain way, and the code shows the way I did it, and it worked before I tried to create and install this diceRoll() method. Also, unlike before, I also got an error saying non-static variable face cannot be referenced from a static context
Here's what I did, trying to put the method outside of the main function ... and PS thanks so much for helping. I'm very confused, but don't want to give up:

import java.util.Random; // Needed for random dice numbers.

public class Dice
{
	int face;
	private int diceRoll()// method to simulate the rolling of one die,
	//generating random number 1 through 6.
	{	
  	// A random object for the program.
 	Random randomNumbers = new Random();
   // Die face number is set equal to a random number.
 	face = randomNumbers.nextInt(6)+ 1;
	return face;
	}
	
	public static void main(String[] args)
	{
	// Display results.
   if (face == 1)
	System.out.println("One:\n\n *");
	else if (face == 2)
	System.out.println("Two:\n*\n\n  *");
	else if (face == 3)
	System.out.println("Three:\n*\n  *\n    *");
	else if (face == 4)
	System.out.println("Four:\n*  *\n\n*  *");
	else if (face == 5)
	System.out.println("Five:\n*   *\n  *\n*   *");
	else if (face == 6)
	System.out.println("Six:\n*  *\n*  *\n*  *");
	
	}
}

Yeah, the static thing can be a drag when you are first starting. One way around it is to basically create some other function within your class and put everything there that you were originally putting in main. You can't do the function calls from main since it is static, so create a new function that isn't static and have it do all the work that you originally intended main to do. You also can't call that function directly from main either so instead you can create a variable of that class in main and have IT call the new function. Finally, Java being Java, it doesn't like it when you don't initialize and use the "new" command, so I did. It actually let me get away without defining a null constructor, but I did it anyway since I think that's a better practice. Of course that function is empty so it does nothing. Thus you are not done yet since you have to somehow assign "face" a value and this program has not (as your last posted program did not either) done so, so you'll need to do that. Basically I don't think you need to touch main after this. Anything you wanted to put in main before, now put in the PlayDiceGame function or somewhere else. Note that you have not called your diceRoll function. This program should compile and run but not really do anything.

Good luck.

import java.util.Random; // Needed for random dice numbers.

public class Dice
{
	int face;
	private int diceRoll()// method to simulate the rolling of one die,
	//generating random number 1 through 6.
	{	
  	// A random object for the program.
 	Random randomNumbers = new Random();
   // Die face number is set equal to a random number.
 	face = randomNumbers.nextInt(6)+ 1;
	return face;
	}
        
        
        public Dice ()
        {            
        }
        
        private void PlayDiceGame ()
        {
	// Display results.
        if (face == 1)
	    System.out.println("One:\n\n *");
	else if (face == 2)
	    System.out.println("Two:\n*\n\n  *");
	else if (face == 3)
	    System.out.println("Three:\n*\n  *\n    *");
	else if (face == 4)
	    System.out.println("Four:\n*  *\n\n*  *");
	else if (face == 5)
	    System.out.println("Five:\n*   *\n  *\n*   *");
	else if (face == 6)
	    System.out.println("Six:\n*  *\n*  *\n*  *");
        }

        
                
	public static void main(String[] args)
	{
            Dice aDie = new Dice ();
            aDie.PlayDiceGame();
	}
}

You have helped me a great deal. I probably should have done this ages ago, but I'd like to include the program code that is supposed to call my diceRoll function. I am not allowed to alter this code at all, per assignment. It's called SnakeEyes, and it successfully calls the diceRoll function, but I still cannot get the display of the faces of the die to work. Do you see anywhere in this SnakeEyes code where it calls for this part of the Dice class that I've created? I know the faces of the die are supposed to display, but I can't get it to work. Should I somehow create another method for that part of the "dice" code? I tried to include it in the diceRoll funtion also, but it was not successful. Here is the SnakeEyes code that's suposed to call my diceRoll() - which I THINK it's supposed to also display the faces of the die that I have in my Dice class:

//********************************************************************
// SnakeEyes.java       
//
// This program rolls a pair of dice 1000 times and counts the number of 
// snake eyes (both dies roll a one) rolled. The Dice class must be used
// to simulate the roll of a die.
//********************************************************************

import java.util.Scanner;

public class SnakeEyes
{
     public static void main (String[] args)
     {
      int rolls, roll1, roll2, noRolls, noSnakeEyes = 0;
      Scanner input = new Scanner(System.in);
			
	   // Create two separate dice objects to create a pair of dice
      Dice die1 = new Dice();
      Dice die2 = new Dice();
		
		// get from the user the number of dice rolls
		System.out.print ("Enter the number of dice rolls: ");
		noRolls = input.nextInt();
		
		// loop for the number of rolls requested by the user
		for (rolls = 1; rolls <= noRolls; rolls++)
  		  {
          System.out.println("_______________Roll #: " + rolls + " _________________");
		    roll1 = die1.diceRoll();
  		    roll2 = die2.diceRoll();

          if (roll1 == 1 && roll2 == 1)
		  	    noSnakeEyes++;
		   }	
				 
         // Display the dice roll results 
         System.out.println ("Number of Dice Rolls: " + noRolls);
         System.out.println ("Number of Snake Eyes Rolled: " + noSnakeEyes);
         System.out.println ("\nGame Ended");            
		
   }
}

If the the diceRoll function produces a random number of 5, it currently returns the integer 5, but does not display anything. Would you like the function diceRoll to return 5, but also display something like

*     *
   *
*     *

to the console screen? Am I understanding the objective? When a die is rolled and a random number from 1 to 6 produced, it should always display that to the screen? And that code should be implemented from the Dice class? Also, "main" will be in SnakeEyes and that is the "main" that you want implemented? And SnakeEyes needs to be altered as little as possible or even not at all? The code to display graphically these numbers 1 through 6 are currently the System.out.println commands currently residing in function PlayDiceGame, correct? Just trying to make sure I understand the goal and the constraints. I believe this is doable with a few modifications to the Dice class but please confirm that I am interpreting the task correctly.

I reread your last post and I think I interpreted it correctly. Yes, I think you can display the die face from diceRoll. You said you tried and it didn't work. I think it can work and I think you can do it from diceRoll and without changing SnakeEyes. If you are running main from SnakeEyes now, you won't need or use the main from Dice so you can get rid of it. Also you can get rid of PlayDiceGame, or rather rename it into something more descriptive like DisplayDieFace. I don't think you'll need to change the contents of it at all.

Then in the function diceRoll, simply make a call to DisplayDieFace right before the "return" statement.

Okay, with your help, I finally did this thing. I was making it more complicated than it was ever supposed to be. I created the Dice class, and the SnakeEyes.java program called on the method diceRoll(). I put the code for the design of the die faces within the diceRoll function and it all worked. I learned much here this weekend. Thank you again. solved.

This question has already been answered. Start a new discussion instead.