Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I had the enemy as a array[] but I recently changed it to an arrayList[] but for some reason nothing shows up anymore, any ideas?
ArrayList<Character> enemy = new ArrayList<Character>(20);
Character enty;
String[] eny = {"Eny_.png" , "eny2.png", "eny3.png"};
for (int i = 0; i < enemy.size(); i ++) {
int ranX = 700 + (int)(Math.random() * 2000);
int ranY = 100 + (int)(Math.random() * 360);
int randE = (int)(Math.random() * 2);
enty = new Character(ranX, ranY,0,0,100,25);
enty.setImage(eny[randE]);
enemy.add(i, enty);
}
for (int i = 0; i < enemy.size(); i ++) {
int ranSpeed = -1 + (int)(Math.random() * -2);
System.out.println(ranSpeed);
enemy.get(i).setVelX(ranSpeed);
enemy.get(i).getVelX();
repaint();
}
}
Because the 20 in
ArrayList<Character> enemy = new ArrayList<Character>(20)
Is the initial capacity, not the initial size. So,
for (int i = 0; i < enemy.size(); i ++) {
is not doing anything because enemy.size() is zero.
The constructor new ArrayList<Character>(20) does not create an ArrayList of size 20, it constructs an empty ArrayList with an initial capacity of 20. So, in your first loop, enemy.size() equals 0.
If you want to add 20 enemies, just use the constant 20 in your loop.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
It has to be output like this 01234
for(int i = 0;i < 5; i++, System.out.print(i));
or
for(int i = 0;i < 5; System.out.print(i))
i++;
Output: 12345
for(int i = 0;i < 5; System.out.print(i))
i++;
Is equivalent to the following while loop:
{
int i = 0;
while (i < 5) {
i += 1;
System.out.print(i);
}
}
i is always incremented before it is printed. Same goes for i++, System.out.print(i): i is already incremented before it is printed. And I don't need to mention that 0 + 1 = 1.
It's pretty easy, you just need to understand what you're writing.
It should go like:
for(int i=0;i<5;i++){ //"i" is increased at the end of the loop
System.out.print(i) //prints "i", which is zero and will increase up to 4
}
And that's it
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a problem with arrays and assigning them to values like float. Here is my code
System.out.print("Please enter the number of iteration: ");
Scanner scn = new Scanner(System.in);
int inumber = scn.nextInt(); // i get the iteration number
Random rnd = new Random();
float x = -10 + rnd.nextFloat() * 20; // i created random number between 10 and -10
System.out.println("My x = " +x);
float[] xarray = new float[4]; // created my test array
Random arrayrnd = new Random();
for (int i=0;i<inumber;i++) { // created a for loop with that number
for (int j = 0; j<xarray.length;j++) {
xarray[j] = arrayrnd.nextFloat(); // also created random for array and assigned them
}
Arrays.sort(xarray); // i sorted the array
xarray[0] = x; // i tried to assign the smallest number to x but didn't work
System.out.println("t="+i+" My new x = " +x);
Also here is my output :
Please enter the number of iteration: 2
My x = -6.2841988
t=0 My new x = -6.2841988
t=1 My new x = -6.2841988
I just don't understand why my x hasn't changed even though I tried to assign the x with the new value. I want my x to change in every turn of the loop and got the the smallest number of the array. But it seems like x never wants to move. I'm sorry if my code is complicated or if I have any mistake. Happy coding!
If you want assign TO x, you should do:
x = xarray[0];
Instead of:
xarray[0] = x;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to populate a deck of cards, and I've come to an odd problem. I assume it has a simple solution but I can't find it for the life of me. Any help would be appreciated.
What's basically happening is, it enters the For loop, and the initialized int is instantly the size of the final required size. Even though it is initialized at 0.
/* Create Deck */
String[] deck = new String[52];
int y, z, i = 0;
for (z = 0; z < 3; z++); // Loop while fills deck array
{
for (y = 0; y < 12; y++);
{
deck[i] = CreateCard(y, z); //Trigger CreateCard method for each card
System.out.println(deck[i]);
i++;
}
}
When the code enters the line deck[i] = CreateCard(y, z);
The values of z is 3 and y is 12. Why does this not run through the entire deck?
You have semi column at the end of the for loop declaration. And you know when we have a semi column at the end of the loop, the body of the loop does not runs with each iteration and when the condition of for loop terminates then the body of the loop executes only once.
Corrected Code
/* Create Deck */
String[] deck = new String[52];
int y, z, i = 0;
for (z = 0; z < 3; z++) // Loop while fills deck array
{
for (y = 0; y < 12; y++)
{
deck[i] = CreateCard(y, z); //Trigger CreateCard method for each card
System.out.println(deck[i]);
i++;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What is wrong with this loop?
int index = 0;
for(int x = 0; x < winDate.length;x++);
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
I've used x several times before in comparing values.
You have got a ";" behind the loop!
In fact this loop does nothing instead of counting.
The part below the loop is getting to initialized statically.
int index = 0;
for(int x = 0; x < winDate.length;x++)-->;<---
{
if(userDate == winDate[x])
{
index = x;
break;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have:
public double[] differences()
{
diffs = new doublae[sequence.length-1];
for (int = 0; i<sequence.length; ++i){
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}
return diffs;
}
However this does not work when i run a test program to check it.
You range should be different - i shouldn't pass sequence.length - 2 in order for i+1 to be a valid index.
double[] diffs = new double[sequence.length-1];
for (int = 0; i < sequence.length - 1; i++) { // changed the condition
double diff = sequence[i+1] - sequence[i];
diffs[i] = diff;
}