![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
lost
hey...
im was writing the code for the game of life..u prob have herd about that ..im almost done...i just need add the code which determines which gameboard the user wants to use...here i got stuck lol..at the very end... im gettin .class expected error here is the sample code with the identical error...... public class test
{
public static void main(String[] args)
{
int sel = 2;
int array[][] = null;
if(sel == 2)
int array[][] = {{2,2},
{2,2}};
else
int array[][] = {{0,0},
{0,0}};
for(int i = 0; i < array.length; i++)
{
System.out.println();
for(int j = 0; j < array[0].length; j++)
{
System.out.print(array[i][j] + " ");
}
}
}
}all i found on the net was that this error is commonly caused by extra, or missing bracket...please help!! thanx |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Dec 2004
Posts: 18
Rep Power: 0
![]() |
are you missing packages or dont you need them for this program?
|
|
|
|
|
|
#3 |
|
Professional Programmer
|
You could do it this way:
public class test
{
public static void main(String[] args)
{
int sel = 2;
int array[][] =new int[2][2];
if(sel == 2)
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
array[i][j] =2;
}
else
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
array[i][j] =0;
}
for(int i = 0; i < array.length; i++)
{
System.out.println();
for(int j = 0; j < array[0].length; j++)
{
System.out.print(array[i][j] + " ");
}
}
System.out.println();
}
}
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#4 |
|
Programmer
|
im sorry i didnt state my question right....im gonna tell u what i want to do...
if the user presses 1 for example i want to initilize the array with certain values...then if user presses 2 i want to initilize the same array with different values...i want to use the same array name in both cases, and i want the case to determine what values this array will be initialized with....i hope this will help u to figure this out... thanks again!! |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
u can put a file of set grid size and values for each data set, plus a blank grid for random population. read the values from the file into the array to populate the "grid".
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#6 |
|
Programmer
|
good idea...but the problem is im not allowed to use file input
![]() |
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
these are the 2 ways of initilising an array in java:
arrayType arrayName[][] = {{row 1},{row2}}; or for a fixed lenght one arrayType arrayName[][] = new arrayType[x][y]; you cant add values in teh {{},{}} way after you have created the initial arry by the looks of things (looked in book), so either use 2 arrays and just use an if statement on the sel so if (sel ==2){
print array1;
}else{
print array2;
}or use xaviars way. |
|
|
|
|
|
#8 |
|
Programmer
|
oki ..i got it ...i hadda use different names for arrays in different cases...i just wonder why it didnt work the way i wanted...anyways, thanks for your help...i really appreciate it...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|