Java 嵌套循环
Java nested loops
计划说明:
Write a program to print 21 rows of X's in the shape of a large X as illustrated below. Be sure so the two rows intersect at the "11" row.
这是我想要的输出:
这是我目前的情况。
public class Program168h {
public static void main (String [] args) {
String d= "X";
for (int a = 1; a < 23; a++) {
for (int b = a; b >= 1; b--) {
System.out.print(" ");
}
System.out.print(d);
for (int x = a; x < 22; x++) {
System.out.print(" ");
}
System.out.print(d);
System.out.println();
}
}
}
这个只生成X的前半部分,下半部分不知道怎么生成。
试试这个:
int xSize = 21;
int ySize = 21;
String sign = "X";
for (int i = 0; i < xSize; ++i) {
for (int j = 0; j < ySize; ++j) {
if (i == j) {
System.out.print(sign);
} else if (i == ySize - j - 1) {
System.out.print(sign);
} else {
System.out.print(" ");
}
}
System.out.println();
}
说明:
第一个对 X 轴坐标进行操作,第二个对 Y 轴进行操作。我们的任务是覆盖对角线。覆盖第一个对角线是 coordinateX == coordinateY。在代码中是 if(i==j)。这些是点 (1,1), (2,2)...... 第二条对角线是点 (x,y)= (20,1),(19,2),(18,3) .. .. 这种情况涵盖第二个 if(i == ySize - j - 1) 。
你可以试试:
public class ProductX {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(" ");
if (i == j) {
System.out.print("X");
}
if(j == 9-i){
System.out.print("X");
}
}
System.out.println();}
}
}
虽然上面的解决方案很完美,但我尝试不使用嵌套 for 进行实验,结果如下。这将比使用嵌套具有更高的性能,其中与 O(n) 相比具有 O(n2) 的复杂性。
public void testXFormation() {
final int countOfLines = 21;
int countOfSpaceBefore = 0;
int countOfSpacesAfter = countOfLines -2 ;// 2 characters
boolean halfReached = false;
for (int index = 0; index < countOfLines; index++) {
printSpaces(countOfSpaceBefore); // print required no. of spaces
System.out.print("x"); // print first x
printSpaces(countOfSpacesAfter); // print required no. of spaces after x
if (index != (countOfLines / 2))// Avoid printing double, in the middle
System.out.print("x");
System.out.println(""); // move to next line
/* Once you reach half matrix we need to reverse the logic */
if (index >= (countOfLines - 1) / 2) {
halfReached = true;
}
/* Reversing the logic for the spaces to be printed */
if (halfReached) {
countOfSpaceBefore--;
countOfSpacesAfter += 2;
} else {
countOfSpaceBefore++;
countOfSpacesAfter -= 2;
}
}
}
private void printSpaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}
计划说明:
Write a program to print 21 rows of X's in the shape of a large X as illustrated below. Be sure so the two rows intersect at the "11" row.
这是我想要的输出:
这是我目前的情况。
public class Program168h {
public static void main (String [] args) {
String d= "X";
for (int a = 1; a < 23; a++) {
for (int b = a; b >= 1; b--) {
System.out.print(" ");
}
System.out.print(d);
for (int x = a; x < 22; x++) {
System.out.print(" ");
}
System.out.print(d);
System.out.println();
}
}
}
这个只生成X的前半部分,下半部分不知道怎么生成。
试试这个:
int xSize = 21;
int ySize = 21;
String sign = "X";
for (int i = 0; i < xSize; ++i) {
for (int j = 0; j < ySize; ++j) {
if (i == j) {
System.out.print(sign);
} else if (i == ySize - j - 1) {
System.out.print(sign);
} else {
System.out.print(" ");
}
}
System.out.println();
}
说明: 第一个对 X 轴坐标进行操作,第二个对 Y 轴进行操作。我们的任务是覆盖对角线。覆盖第一个对角线是 coordinateX == coordinateY。在代码中是 if(i==j)。这些是点 (1,1), (2,2)...... 第二条对角线是点 (x,y)= (20,1),(19,2),(18,3) .. .. 这种情况涵盖第二个 if(i == ySize - j - 1) 。
你可以试试:
public class ProductX {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(" ");
if (i == j) {
System.out.print("X");
}
if(j == 9-i){
System.out.print("X");
}
}
System.out.println();}
}
}
虽然上面的解决方案很完美,但我尝试不使用嵌套 for 进行实验,结果如下。这将比使用嵌套具有更高的性能,其中与 O(n) 相比具有 O(n2) 的复杂性。
public void testXFormation() {
final int countOfLines = 21;
int countOfSpaceBefore = 0;
int countOfSpacesAfter = countOfLines -2 ;// 2 characters
boolean halfReached = false;
for (int index = 0; index < countOfLines; index++) {
printSpaces(countOfSpaceBefore); // print required no. of spaces
System.out.print("x"); // print first x
printSpaces(countOfSpacesAfter); // print required no. of spaces after x
if (index != (countOfLines / 2))// Avoid printing double, in the middle
System.out.print("x");
System.out.println(""); // move to next line
/* Once you reach half matrix we need to reverse the logic */
if (index >= (countOfLines - 1) / 2) {
halfReached = true;
}
/* Reversing the logic for the spaces to be printed */
if (halfReached) {
countOfSpaceBefore--;
countOfSpacesAfter += 2;
} else {
countOfSpaceBefore++;
countOfSpacesAfter -= 2;
}
}
}
private void printSpaces(int count) {
for (int i = 0; i < count; i++)
System.out.print(" ");
}