简体   繁体   中英

User specified 2d array element values

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                while (treasures >= 0) {
                    mapArray[i][j] = rnd.nextInt(2);
                    treasures -= 1;
                }
            }
        }

The user specifies the array's height and width, along with how many "treasures" this array contains. The code should cycle through all of the array's elements, giving them a value of either 0 or 1 ( until the number of treasures entered by the user reaches 0 ).

Treasure indication is 1.

Right now the for loops only target the first ( [0] [0] ) element.

You should eliminate the while loop, since it prevents i and j from being incremented until it ends, which is why only mapArray[0][0] is assigned.

    for (int i = 0; i < height && treasures >= 0; i++) {
        for (int j = 0; j < width && treasures >= 0; j++) {
            mapArray[i][j] = rnd.nextInt(2);
            treasures -= 1;            
        }
    }

Note that if treasures < height * width , some elements of the array will contain 0 by default.

Remove the while loop, as it forces to only run for mapArray[0][0] and while loop ends when treasures become zero. This eventually terminates all the loops.

I think number of "treasures" is associated with "1". So if the user wants the array's height to be 4 and width also 4 and "treasures" to be for example 3 i would expect something like this: three "1" and rest "0"

     0110
     1000
     0000
     0000

in that case you may try

Random rnd = new Random();
    int [][] grid = new int[height][width];
    int treasures = 3;

        for (int[] row : grid){                
            for (int n : row){                    
                if(treasures>0){
                    n = rnd.nextInt(2);
                    if(n==1){
                       treasures -= 1;
                    }
                }
                System.out.print(n);
            }
          System.out.println();
        }

To understand your code, I encourage you to take a pen and write what your code do.

For example :

  • Input height=2, width=3, treasures=1
  • Step 1 i=0 and height>i => 2>1 => continue (height=2, width=3, treasures=1, i=0)
  • Step 2 j=0 and width>j => 3>0 => continue (height=2, width=3, treasures=1, i=0, j=0)
  • Step 3 treasure >= 0 => 1>=0 => continue
  • Step 4 mapArray[i][j] = rnd.nextInt(2); => mapArray[0][0] = rnd.nextInt(2);
  • Step 5 treasures -= 1 => treasure = 0 (height=2, width=3, treasures=0, i=0, j=0)
  • Step 6 treasure >= 0 => 0>=0 => continue [I think you don't want this but it's not the problem that causes your question]
  • Step 7 mapArray[i][j] = rnd.nextInt(2); => mapArray[0][0] = rnd.nextInt(2); ...

Now a few hints to continue :

The code should cycle through all of the array's elements, giving them a value of either 0 or 1 ( until the number of treasures entered by the user reaches 0 ).

You can translate it into : I need to place my X treasures into my map.

So the while loop is a good idea, but it's actually in a wrong place. Delete the 2 for loops and keep only the while loop to begin with.

Now think and find a way to put these treasures, one by one, in the right place on your map.

Later, if you need to run through you map, then you will need your 2 for loops. It can be usefull to display your map for example.

Don't forget to check that your inputs are valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM