简体   繁体   English

输入 int <= 0 后,如何阻止二维 int 数组输出其值?

[英]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;当用户输入值 <= 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.在代码中使用break语句。

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.当循环中遇到 break 语句时,循环立即终止,程序控制在循环后面的下一条语句处恢复。

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();
       }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 CSV 文件中的 int 值存储在单独的二维数组元素中 - How do I store int values from CSV file in separate two-dimensional array elements 如何将2字符串数组维数组的某些值转换为int并将它们与Java中的另一个整数进行比较? - How do I convert certain values of a 2 string array dimensional array to int and compare them with another integer in java? 固定的二维Int数组 - Two Dimensional Int array, fixed, 使用openCSV忽略CSV值+将CSV文件中的int值存储在单独的二维数组元素中 - Ignoring CSV values with openCSV + Storing int values from CSV file in separate two-dimensional array elements 编写一个名为 arraySum 的 function,它返回 int 值的二维数组中所有值的总和(作为 int)(Java) - Write a function called arraySum that returns the sum (as an int) of all the values in a two-dimensional array of int values (Java) 二维整数数组不接受我提供的尺寸 - Two-dimensional int array is not accepting the dimensions I am providing 如何将字符串的二维数组转换为 int 类型之一? - How to convert two dimensional array of string into one of type int? 如何使用流将二维int数组转换为单个字符串? - How to convert a two dimensional int array to a single string using streams? 如何创建具有两个int值的二叉树? - How do I create a binary tree with two int values? 如何引用用户输入的int? - How do I reference a user-entered int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM