Two dimensional array as buffer for screen Learn programming Java

Lesson:

Arrays, Structures and Strings


Exercise:

Two dimensional array as buffer for screen


Objetive:

Create a java program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays the content of the array on screen.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		char[][] position = new char[20][70];

		Random generator = new Random();
		int i = 0;

		for (; i < 80; i++)
		{
			position[generator.nextInt(20)][generator.nextInt(70)] = 'X';
		}

		for (i = 0; i < 20; i++)
		{
			for (int j = 0; j < 70; j++)
			{
				System.out.print(position[i][j]);
			}

			System.out.println();
		}
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.