Home > Enterprise >  Java array filling
Java array filling

Time:02-26

In this code I generate 100 tiles with different values (lust, anger...) and print them in 10 groups of 10 elements.

The issue is that I would like to print each tile value it in a 10x10 grid instead.

How can I fill up the example grid (attached) with the desired tile values?

Format how I want the grid to look like:

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
//For loop to generate 100 tiles
for(int i = 0; i <= 100; i  ){

   //Generate a number between 0 - 100 (based on percentages) to find which tile to place
   double percentChanceRandom1 = r.nextDouble(100);
   double percentChanceRandom2 = r.nextDouble(100);
   double percentChanceRandom3 = r.nextDouble(100);
   double percentChanceRandom4 = r.nextDouble(100);
   double percentChanceRandom5 = r.nextDouble(100);

   //System.out.println(percentChanceRandom);

   //Determine the tile square value
   if(percentChanceRandom1 <= percentChanceLust){
       tile = "Lust";
   }
   else if(percentChanceRandom2 <= percentChanceAnger){
       tile = "Anger";
   }
   else if(percentChanceRandom3  <= percentChanceIdol){
       tile = "Idolatry";
   }
   else if(percentChanceRandom4 <= percentChanceVanity){
       tile = "Vanity";
   }
   else if(percentChanceRandom5 <= percentChanceSpeech){
       tile = "Speech";
   }
   else {
       tile = "Physical";
   }

   if(i == 0){
     System.out.println(tile);
     System.out.println("\n");
   }
   //Print out which tile was chosen
   else{
     System.out.println(tile);
   }
}//End For

CodePudding user response:

It looks like you can just use a nested loop.

for(int i = 0; i < 10; i  ){
    for(int j = 0; j < 10; j  ){
        /* write code to add a single value */
    }
}

CodePudding user response:

You can use two loops and only generate integers to get random values.

List<String> values = Arrays.asList("Lust", "Anger", "Idolatry", "Vanity", "Speech", "Physical");
Random rand = new Random()
for(int i = 0; i < 10; i  ){
    for(int j = 0; j < 10; j  ){
        
        System.out.print(values.get(rand.nextInt(values.size())));
    }
    System.out.print("\n");
}

CodePudding user response:

As Ariel mentioned you can use a nested for loop to store the values of tile in a 2D array.

Since we have 100 values total and we want to group them by 10s, we should have a 2D array that is 10 by 10 that stores Strings:

String[][] emotions = new String[10][10];

To iterate through emotions and fill it up, we can use a nested for loop with one counter to iterate through the "width" and the other through the "height":

for(int i = 0; i < 10; j  ){
   for(int j = 0; j < 10; j  ){
       //defining tile
       emotions[i][j] = tile;
   }
}

This will allow for us to fill up emotions as our loops go from an i-j pair of 0-0 to an i-j pair of 9-9.

Now, we can also print out our array to output these values by using a for-each loop:

for(String[] i: emotions){
   for(String j: i){
      System.out.print(j   " ");
   }
   System.out.println();
}

What this code does is: "For every sub-array i, in our 2D array emotions, and for every String j in our sub-array i, we can print out j. After we print out all the Strings j in i, we can print out a newline and move on to our next sub-array i in emotions.

Overall, the code should look something like this:

String[][] emotions = new String[10][10];
//For loop to generate 100 tiles
for(int i = 0; i < 10; j  ){
    for(int j = 0; j < 10; j  ){
        double percentChanceRandom1 = r.nextDouble(100);
        double percentChanceRandom2 = r.nextDouble(100);
        double percentChanceRandom3 = r.nextDouble(100);
        double percentChanceRandom4 = r.nextDouble(100);
        double percentChanceRandom5 = r.nextDouble(100);
    
        //System.out.println(percentChanceRandom);
    
        //Determine the tile square value
        if(percentChanceRandom1 <= percentChanceLust){
            tile = "Lust";
        }
        else if(percentChanceRandom2 <= percentChanceAnger){
            tile = "Anger";
        }
        else if(percentChanceRandom3  <= percentChanceIdol){
            tile = "Idolatry";
        }
        else if(percentChanceRandom4 <= percentChanceVanity){
            tile = "Vanity";
        }
        else if(percentChanceRandom5 <= percentChanceSpeech){
            tile = "Speech";
        }
        else {
            tile = "Physical";
        }
    
        emotions[i][j] = tile;
    }
}//End For

for(String[] i: emotions){
    for(String j: i){
    System.out.print(j   " ");
    }
    System.out.println();
}

I hope this helped! Let me know if you need any further clarification or details :)

  • Related