My program loops through the array at its final stage using 2 for loops, One prints out 10 blank new lines the other prints using a for loop and does this 5 times i end up with a 5 x 10 table.
What i need to do now is figure a way to implement this in the main while loop so that the numbers are printed in a table format as they are generated. Rather than ging through the array again.
Here is my code:
/*
Program Title: Random50
Author: gpjones20
Created: 18 November 2012
Version: 1.0
*/
//random Imported To Program
import java.util.Random;
public class Random50
{
public static void main(String args[])
{
//Creating New Random Object
Random randomNum = new Random();
int numbersArray [] = new int[50];
int count = 0;
System.out.println("");
//Loop to Verify That No Matching numbers Are Added To Array
while (count < 50)
{
int nextRandomVal = randomNum.nextInt(999) + 1;
Boolean matchCheck = false;
for (int i = 0; i < count; i++)
{
//Checking For Equallity of Array Values With new radnom number
if (numbersArray[i] == nextRandomVal)
{
matchCheck = true;
//Un Comment Below Statement To See Matches While Prgram Is Running
//System.out.println("A Match Has Been Found " + numbersArray[i]);
break;
}
}
//Adding Random Values To Array
if (!matchCheck)
{
numbersArray[count] = nextRandomVal;
count++;
}
}
//Printing Message On To Console For User
System.out.println("");
System.out.println("Random Numbers Printing...........");
System.out.println("");
//Printing The Array Contents Table Format
//Loop To print 10 Lines Of Print Statements
for (int row = 0; row < 10; row++)
{
//Printing 5 indexes from the Array
for(int column = 0; column < 5; column++)
{
System.out.printf("%03d ",numbersArray[row * 5 + column]);
}
//Printing a new blank line
System.out.println();
}
}
}
//END OF PROGRAM

New Topic/Question
Reply



MultiQuote






|