[英]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.