I'm trying to instantiate three objects in a loop so that I don't reuse the constructor text. This is because, for Uni, we get marked down if we reuse the same line of code (it's marked automatically).
My code is as follows:
EnemyShip enemy1;
EnemyShip enemy2;
EnemyShip enemy3;
public Game()
{
for (int i = 1; i <= 3; i++) {
getEnemyRef(i) = new EnemyShip(); //getEnemyRef unexpected type - required:variable found:value
//enemy1 = new EnemyShip(); works normally
}
}
The i is underlined and I am told that it is a value not variable.
Please note that I use the following code instead of a list or array because we are not allowed to use them for this task.
public EnemyShip getEnemyRef(int enemy) {
switch (enemy)
{
case 1:
return enemy1;
case 2:
return enemy2;
case 3:
return enemy3;
}
return null;
}
The marking system bases how many times a line is reused from how many times it is written in source code, not how many times it is executed