1 Replies - 397 Views - Last Post: 07 August 2012 - 08:44 PM Rate Topic: -----

#1 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

randomNumber cannot be resolved to a variable - 2d Array

Posted 07 August 2012 - 08:04 PM

Hi. I've been given a task of creating a two-dimensional array that generates 3 integers: 0, 1 or 2. Through some searching on the internet, I've managed to put bits and pieces together (not the best way to go about things), but to no avail. I can print 19x19 integers out in the rows and columns, but I only get 0's. I know how to get random numbers without an array between a set of numbers with the Math.random method, but I can't apply the same to the array.

import java.util.*;
public class RandomArray
{
	public static void main(String[] args)
	public class RandomArray
{
	public static void main(String[] args)
	{
		 final int ROWS = 19;
		 final int COLS = 19;
		
		int[][] a2 = new int[19][19];
	
		//... Print array in rectangular form
		for (int i =0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			
			int randomNumber = (int) (Math.random()*(2-0) + 1);
		System.out.print(" " + a2[i][j]);
		}
		System.out.println(randomNumber);
		}
		
	}
}

}


I'm fairly new to programming/Java, so please forgive me for anything stupid I've done. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: randomNumber cannot be resolved to a variable - 2d Array

#2 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 428
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: randomNumber cannot be resolved to a variable - 2d Array

Posted 07 August 2012 - 08:44 PM

You are not assigning the variables in your array to anything. You get a random number and save it to the variable randomNumber, but then you don't change your array. After you get randomNumber, try:

a2[i][j] = randomNumber;

Edit: As a side lesson, what does this tell you about the default values for integers (When you don't explicitly assign them)?

This post has been edited by Sheph: 07 August 2012 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1