Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 What is wrong with this simple java program?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Army_Hermet6312




PostPosted: Wed Sep 10, 2008 9:46 am   Post subject: What is wrong with this simple java program?

I am attempting to make a simple program that will make a random bingo card for the user but i keep getting an error when i try to initalize the variables with random numbers, what am i doing wrong?
// The "Exercise1" class.
import java.awt.*;
import hsa.Console;
import java.lang.Math;

public class Exercise1
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();
// fist create arrays for each of the bingo sections
int B [] = new int [5];// Place your program here. 'c' is the output console
int I [] = new int [5];
int N [] = new int [5];
int G [] = new int [5];
int O [] = new int [5];
// the first thing we must do is initalize all the numbers with random integers
for (int i = 1; i <=5; i++)
{
B(i) = (int)(15 * Math.random () + 1);
}
} // main method
} // Exercise1 class
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Wed Sep 10, 2008 10:58 am   Post subject: RE:What is wrong with this simple java program?

If memory serves correctly, you have two errors.


  1. Array indexes start at 0, not one. Change your for loop to read like the following:
    Java:

    for (int i = 0; i < B.length; i++)

  2. You access the elements of an array with square brackets, not parenthesis.
    Java:

    B[i] = (int)(15 * Math.random () + 1);



Oh, and in the future please use code or syntax tags like so:
[ syntax = "Java" ] [ /syntax]
Without the spaces of course. Java is painful enough to read as it is, it might as well be colourful.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: