嵌套 For 循环,替换 If 语句
Nested For Loop, Replace If statement
我最近一直在练习我的嵌套for循环。我尝试构建一个从 (0,0) 到 (5,5) 的 5x5 坐标块。每行将上升到 y = 5,然后开始新的一行。
这是我构建的代码,它做我想做的事,唯一的问题是我必须使用 if 语句。是否有另一个 for 循环我可以用来制作它,这样我就不必使用 if 语句?
public class NestedLoops3 {
public static void main(String[] args) {
for(int counter =0;counter<6;counter++) {
for(int counter2 = 0;counter2<6;counter2++)
{
System.out.print("("+counter+","+counter2+")");
if(counter2==5) {
System.out.println();
}
}
}
}
}
现在它按照我的预期进行,但我只是想看看我是否可以用另一个 for 循环替换 if 语句。
是的。在嵌套循环之后移动 println
。喜欢,
for (int counter = 0; counter < 6; counter++) {
for (int counter2 = 0; counter2 < 6; counter2++) {
System.out.print("(" + counter + "," + counter2 + ")");
}
System.out.println();
}
在嵌套 for 循环外调用 System.out.println();
我不建议使用 for 循环编写 if 语句:)
在循环工作后写入新行时,对于方形网格,您可以使用单个循环并计算行和列
int size = 5;
for(int counter =0;counter<(size*size)+1;counter++) {
int col = counter % size;
int row = counter / size;
System.out.printf("(%d, %d) ", row, col);
if(col == size) {
System.out.println();
}
}
是的,有可能:
public class NestedLoops3 {
void main(String[] args) {
for(int counter =0;counter<6;counter++) {
for(int counter2 = 0;counter2<6;counter2++) {
System.out.print("("+counter+","+counter2+")");
for (; counter2 == 5; ) {
System.out.println();
break;
}
}
}
}
}
但我看不出这样做的理由。 for-loop 的中间部分是一个条件,可用于该目的。为了避免副作用,我放弃了将 counter2 设置为 6,例如,在没有 break 语句的情况下中断循环,但是由于 counter2 的范围有限,这也是有效的:
for(int counter2 = 0;counter2<6;counter2++) {
System.out.print("("+counter+","+counter2+")");
for (; counter2 == 5; counter2=6) {
System.out.println();
}
}
由于这种样式非常少见,容易出错,如果有人尝试扩展代码,例如将中间循环设置为 运行 7。
但有可能,是的。
输出:
-> main (null)
(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)
(1,0)(1,1)(1,2)(1,3)(1,4)(1,5)
(2,0)(2,1)(2,2)(2,3)(2,4)(2,5)
(3,0)(3,1)(3,2)(3,3)(3,4)(3,5)
(4,0)(4,1)(4,2)(4,3)(4,4)(4,5)
(5,0)(5,1)(5,2)(5,3)(5,4)(5,5)
我最近一直在练习我的嵌套for循环。我尝试构建一个从 (0,0) 到 (5,5) 的 5x5 坐标块。每行将上升到 y = 5,然后开始新的一行。
这是我构建的代码,它做我想做的事,唯一的问题是我必须使用 if 语句。是否有另一个 for 循环我可以用来制作它,这样我就不必使用 if 语句?
public class NestedLoops3 {
public static void main(String[] args) {
for(int counter =0;counter<6;counter++) {
for(int counter2 = 0;counter2<6;counter2++)
{
System.out.print("("+counter+","+counter2+")");
if(counter2==5) {
System.out.println();
}
}
}
}
}
现在它按照我的预期进行,但我只是想看看我是否可以用另一个 for 循环替换 if 语句。
是的。在嵌套循环之后移动 println
。喜欢,
for (int counter = 0; counter < 6; counter++) {
for (int counter2 = 0; counter2 < 6; counter2++) {
System.out.print("(" + counter + "," + counter2 + ")");
}
System.out.println();
}
在嵌套 for 循环外调用 System.out.println();
我不建议使用 for 循环编写 if 语句:)
在循环工作后写入新行时,对于方形网格,您可以使用单个循环并计算行和列
int size = 5;
for(int counter =0;counter<(size*size)+1;counter++) {
int col = counter % size;
int row = counter / size;
System.out.printf("(%d, %d) ", row, col);
if(col == size) {
System.out.println();
}
}
是的,有可能:
public class NestedLoops3 {
void main(String[] args) {
for(int counter =0;counter<6;counter++) {
for(int counter2 = 0;counter2<6;counter2++) {
System.out.print("("+counter+","+counter2+")");
for (; counter2 == 5; ) {
System.out.println();
break;
}
}
}
}
}
但我看不出这样做的理由。 for-loop 的中间部分是一个条件,可用于该目的。为了避免副作用,我放弃了将 counter2 设置为 6,例如,在没有 break 语句的情况下中断循环,但是由于 counter2 的范围有限,这也是有效的:
for(int counter2 = 0;counter2<6;counter2++) {
System.out.print("("+counter+","+counter2+")");
for (; counter2 == 5; counter2=6) {
System.out.println();
}
}
由于这种样式非常少见,容易出错,如果有人尝试扩展代码,例如将中间循环设置为 运行 7。
但有可能,是的。
输出:
-> main (null)
(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)
(1,0)(1,1)(1,2)(1,3)(1,4)(1,5)
(2,0)(2,1)(2,2)(2,3)(2,4)(2,5)
(3,0)(3,1)(3,2)(3,3)(3,4)(3,5)
(4,0)(4,1)(4,2)(4,3)(4,4)(4,5)
(5,0)(5,1)(5,2)(5,3)(5,4)(5,5)