Java – initializes multiple variables in a for loop

I am a student trying to find out how to solve a seemingly simple problem When I try to initialize 2 variables in a for loop, I get an error I'm creating a bank for the game board Why did I get this mistake?

This is the method:

public String [] board;

public void printBoard(){
            for(int i,j = 0; i < this.board.length; i++,j++)
                if(j > 10)
                    System.out.println();
                else
                    System.out.print(this.board[i]);

> java:39: error: variable i might not have been initialized

Solution

This is because you did not initialize the variable I, which could be zero or something else

for(int i = 0,j++)
            if(j > 10)
                System.out.println();
            else
                System.out.print(this.board[i]);

Don't forget to initialize variables if some objects are using it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>