User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 429,896 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,442 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 798 | Replies: 5
Join Date: May 2008
Posts: 2
Reputation: geisteskrankhei is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geisteskrankhei geisteskrankhei is offline Offline
Newbie Poster

random numbers into an array

  #1  
May 13th, 2008
Basically, I have to make a code that requests user input. The program asks them to tell me how many columns/rows the user wants the magic square to have. I need to then generate random numbers for each spot.

ex: user inputs 3, out put looks like

3 7 1
6 4 9
8 2 5

I know how to generate a single random number but I've been trying to figure out how I'm going to get as many as the user needs.

Numbers cannot be duplicated, but I can tackle that if statement on my own easily. it's just the part that involves multiple random numbers put into an array that I am having difficulty on. Any help would be greatly appreciated. So far, my code looks like:

 import java.awt.*;
import hsa.Console;
import java.util.Random;

public class MagicSquare
{
    static Console c;           

    public static void main (String[] args)
    {
        c = new Console ();

        Random random = new Random ();
        c.println ("How many numbers up/down and left/right would you like in your magic square?");
        int size = c.readInt ();
        int NumberOfRandoms = (size * size);
        int myRandomNumber = random.nextInt (NumberOfRandoms) + 1;
        c.println (myRandomNumber);

        
    } 
} 
Last edited by geisteskrankhei : May 13th, 2008 at 6:45 pm.
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:04 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC