I'm working on a simple matching game, whereby my array includes 3 numbers:{ 5,9,0 } and the user tries to guess them.

Instead, I want to use the Random class to generate 3 different numbers from 0-9, but I'm not sure how or where to start. I came across info on the Random class; however, there are no good examples using it with arrays. But, then I'm awful at arrays and need improvement in this area.

Can anyone help me out in this area? Thanks in advance.

import java.io.*;
import javax.swing.JOptionPane;

public class MatchGame
{
		public static int[] guess = {5,9,0};
		public static void main(String[] args) throws IOException
	
		{
		MatchGame();
		}

		public static void MatchGame()
		{
		int o = 3;
		boolean arraysMatch = true;
		String input;
		int i = 0;
		int[] x = new int[o];
		for (i = 0; i < o; i++)
		{
		input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1));
		x[i] = Integer.parseInt(input);
		}
		i = 0;
				if (guess.length != x.length)
				arraysMatch = false;

				while (arraysMatch && i < guess.length)
		{
				if (guess[i] != x[i])
				arraysMatch = false;
				i++;
		}
				if (arraysMatch)
				JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers.");
		else
				JOptionPane.showMessageDialog(null, "Sorry, you didn't match!");

		results();
		}
		public static void results()
		{
			JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] +
			guess[1] + guess[2]);
		}

}

To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.

To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.

To construct a single random object at the start, etc., do you mean replace:

public static int[] guess = {5,9,0};

with:

int[] guess = new int[10];
for (int i = 0; i < 10; i++)
guess = i;
public static int[] guess = {5,9,0};

with:

int[] guess = new int[10];
for (int i = 0; i < 10; i++)
guess = i;

There's nothing random about this. Three questions you need to decide. One, how many elements should be in the array? Two, what is the range of these random numbers? Three, can there be any repeats in the array?

I interpreted the answers to questions 1 and 2 as 3 and 10, respectively, from post 1, so change the loop from:

int[] guess = new int[10];
for (int i = 0; i < 10; i++)
guess = i;

to

int[] guess = new int[3];
for (int i = 0; i < 3; i++)
{
    // code
}

You definitely don't want the guess = i; line, since that's not random.

Create a Random object before the loop.

Random rand = new Random ();

Inside the loop, create the random numbers and fill the array.

Random rand = new Random ();
 int[] guess = new int[3];
for (int i = 0; i < 3; i++)
{
     guess[i] = rand.nextInt (10);
}

Depending on your answer to whether you allow repeats, the code could be a little more involved, but if you don't care, the above is the idea if I'm reading your problem correctly.

I appreciate the help, but you lost me.

1st ? - Do I remove:

public static int[] guess = {5,9,0};

2nd ? - Where do I insert Random object and related loop?

I'm getting errors out the bazoo, no matter what or where I try.

Thanks for any suggestions.

I appreciate the help, but you lost me.

1st ? - Do I remove:

public static int[] guess = {5,9,0};

Yes. You are declaring guess here in my example:

int[] guess = new int[3];

so you can't declare it twice.

2nd ? - Where do I insert Random object and related loop?

Probably make both of them non-static class variables and instantiate them in your constructor:

import java.util.Random;

public class MatchGame 
{
    Random rand;
    int[] guess;
    
    public MatchGame() 
    {
        rand = new Random ();
        guess = new int[3];
        for (int i = 0; i < 3; i++)
        {
            guess[i] = rand.nextInt (10);
        }

        // more code
    }

    // more MatchGame class functions here
    
    
    public static void main (String args[])
    {
        new MatchGame ();
    }    
}

I end up with 11 errors after I revise my code. Did I miss something? Thanks in advance.

import java.util.Random;
public class MatchGame
{
      Random rand;
      int[] guess;
      
      public MatchGame()
      {
         rand = new Random();
         guess = new int[3];
         for (int i = 0; i < 3; i++)
         {
            guess[i] = rand.nextInt (10);
         }
   
      {
      MatchGame();
      }

      public static void MatchGame()
      {
      int o = 3;
      boolean arraysMatch = true;
      String input;
      int i = 0;
      int[] x = new int[o];
      for (i = 0; i < o; i++)
      {
      input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1));
      x[i] = Integer.parseInt(input);
      }
      i = 0;
            if (guess.length != x.length)
            arraysMatch = false;

            while (arraysMatch && i < guess.length)
      {
            if (guess[i] != x[i])
            arraysMatch = false;
            i++;
      }
            if (arraysMatch)
            JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers.");
      else
            JOptionPane.showMessageDialog(null, "Sorry, you didn't match!");

      results();
      }
      public static void results()
      {
         JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] +
         guess[1] + guess[2]);
      }
         
         public static void main (String[]args)
         {
         new MatchGame ();
      }
}
}

ÏÏÏ ----jGRASP exec: javac -g C:\Program Files\MatchGame.java
ÏÏÏ
ÏÏMatchGame.java:34: illegal start of expression
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:34: ';' expected
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
Ï Ï public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
Ï MatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: '.class' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏÏ11 errors

Start with my skeleton, compile it, run it without errors, then put in your code a little bit at a time. Look at your brackets, look at your main function, look at your indentation. Remove any brackets that don't serve a purpose, indent consistently so you know what code is part of which section of brackets. Look at anything in your code that says "static" and if you don't know why you are making it static, don't make it static. main has to be static, but for everything else, if you don't need to make it static, don't. If you have two functions with the same name, rename one. If a function is not called, comment it out.

You cannot debug code until you have consistent indentation. It will really help you see what's wrong.

This article has been dead for over six months. Start a new discussion instead.