My Code:
import java.util.Scanner; import java.util.Random; public class turtle { public static void main(String args[]){ Scanner input = new Scanner(System.in); Random rand = new Random(); int grid[][] = new int[5][5], poschance, row = 0, column = 0, cont = 1; while(cont == 1){ for(int i = 0; i <= 1000; i++){ poschance = rand.nextInt(3 - 1 + 1) + 1; switch(poschance){ case 1: if(row == 4){ row--; } else{ row++; } break; case 2: if(column == 4){ column--; } else{ column++; } break; case 3: if((row == 4)&&(column == 4)){ row--; column--; } else{ row++; column++; } break; } grid[row][column]++; } row = 0; column = 0; while(row <= 4){ while(column <= 4){ System.out.printf("%d", grid[row][column]); column++; } System.out.print("\n"); row++; } System.out.print("\nDo you want to continue?\n1. yes\n2.no\n"); cont = input.nextInt(); } } }
Runtime Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at turtle.main(turtle.java:45)
What I am trying to achieve:
I want to see how randomly I can paint the grid of matrices 5x5 in 100 turns.
Last word:
Please help!