简体   繁体   中英

How do I stop a two dimensional int array from outputting its values once an int <= 0 is entered?

the method with an array:

void outputBock(int [][] block) 

The first Dimension saves blocks, the second Dimension numbers that are in the blocks.

(Numbers in the block are saved by the input in the console) (function for that already there.)

When the user types in a value <= 0; the Array has to stop printing the number from within the block after that specific number.

The two-dimensional Array:

 {{1, 2, 3, 4, 5},
 {6, 7, 8, 9, 10},
 {18, 15, 17, 19, 14},
 {26, 47, 58, 59, 60}}

is being printed like this:

  block 1: 1 2 3 4 5
  block 2: 6 7 8 9 10
  block 3: 18 15 17 19 14
  block 4: 26 47 58 59 60

The two dimensional Array:

  {{3, 2, 1, 0, -1},
  {9, 7,11 12, 13, 14},
  {15, 16, 17, 108, 19},
  {20, 21, 22, 23, 24}}

is being printed like this:

  block 1: 3 2 1
  block 2: 9 7 11 12 13 14
  block 3: 15 16 17 18 19
  block 4: 20 21 22 23 24

Any idea how I can stop the array from printing out numbers within a block once something <= is typed in?

Use break statements in your code.

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

void print(){

       int[][] array = {
                         {3, 2, 1, 0, -1},
                         {9, 7,11 ,12, 13, 14},
                         {15, 16, 17, 18, 19},
                         {20, 21, 22, 23, 24}
                       } ;
       for(int i=0;i<array.length;i++) {
           System.out.print("block "+ (i+1)+" ");//Prints the block number
           for(int j=0;j<array[i].length;j++) {
               if(array[i][j] <= 0) {
                   break; 
                   //Goes to the next iteration of outer for loop.
               }
               else {
                   System.out.print(array[i][j]+" ");
               }
           }
           System.out.println();
       }
}

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