This site requires JavaScript, please enable it in your browser!
Greenfoot back
tayyabah72
tayyabah72 wrote ...

2021/5/23

Creating array to store 100 student ids

tayyabah72 tayyabah72

2021/5/23

#
I am wondering how could we create an array to store 100 student ids so i know we need to use loop for this but not sure how it will work. Can anyone help with that?
rocket770 rocket770

2021/5/23

#
To initialize the array (assuming you are storing student ID's as integers):
1
int[] studentIDs = new int[100]; // Integer array named "studnetIDs" of size 100
To loop through it
1
2
3
for (int i = 0;  i < studentIDs.length; i++) {
            // now the ith index in studentIDs can be referenced as  studentIDs[i]
 }
tayyabah72 tayyabah72

2021/5/27

#
can i display these ids on screen?
tayyabah72 tayyabah72

2021/5/27

#
How can we make 4 arrays instead of the loop to create this same method of storing 100 student ids?
danpost danpost

2021/5/27

#
tayyabah72 wrote...
How can we make 4 arrays instead of the loop to create this same method of storing 100 student ids?
Why would you want to create 4 arrays instead of 1?
tayyabah72 tayyabah72

2021/5/28

#
I just wanted to find out different ways of doing the job.
danpost danpost

2021/5/28

#
tayyabah72 wrote...
I just wanted to find out different ways of doing the job.
Well, okay; but there should be a reason for dividing up the ids. Let's say you had one, like maybe you have 4 classrooms of 25 students each (classrooms 1, 2, 3 and 4) and you are listing which student is in which room. Then
1
int[][] studentDistribution = new int[4][25];
and looping through, displaying on screen (after loading ids into the array in similar manner) would be (in a world of least 600 x 560):
1
2
3
4
5
6
7
8
for (int j=0; j<4; j++)
{
    showText("Room "+(j+1), 100+j*125, 20);
    for (int i=0; i<25; i++)
    {
        showText(""+studentDistribution[j][i], 100+j*125, 60+i*20);
    }
}
You need to login to post a reply.