[英]Java Loops and If conditions for creating X box
enter image description here How to print a big X made up of small X Box which I have provided the code below.在此处输入图像描述如何打印由小 X Box 组成的大 X,我在下面提供了代码。 The size of the X must be equal to the size of the small X Box. X 的大小必须等于小 X Box 的大小。
System.out.print("Enter the desired cube size: ");
int size = new Scanner(System.in).nextInt();
for(int row=1; row<=size; row++) {
for(int col=1; col<=size; col++) {
if (row == 1 || row == size || col == 1 || col == size || col == row ||col == size-(row-1)) {
System.out.print("X ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
The output should look like this. output 应该是这样的。 The size of the big X must be equal to the small one.大 X 的大小必须等于小 X。 Like this, the given size is 3.像这样,给定的大小是 3。
X X X X X X
X X X X
X X X X X X
X X X
X X
X X X
X X X X X X
X X X X
X X X X X X
If I have not misunderstood you, when you insert the size keyboard you should always print five mini "x" in size -> size * size.如果我没有误解你,当你插入尺寸键盘时,你应该总是打印五个迷你“x”尺寸 -> 尺寸 * 尺寸。 By retouching your code, I changed the loops for(), size*3, and the line of the if..else.通过修改您的代码,我更改了 for()、size*3 和 if..else 行的循环。 Where if (), if we have a square, we will divide it into 9 parts.其中if(),如果我们有一个正方形,我们将它分成9个部分。 If we are in the corners or in the center, we will print on screen "X ", if not, " ".如果我们在角落或中心,我们将在屏幕上打印“X”,如果不是,“”。
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter the desired cube size: ");
int size = new Scanner(System.in).nextInt();
for(int row=0; row<(size*3); row++) {
for(int col=0; col<(size*3); col++) {
if ((row/size!=1 && col/size!=1) || (row/size==1 && col/size==1)) {
System.out.print("X ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
This will return the following content: program exit这将返回以下内容:程序退出
X X X X X X
X X X X X X
X X X X X X
X X X
X X X
X X X
X X X X X X
X X X X X X
X X X X X X
What I have not implemented is the white space in the center of the small squares.我没有实现的是小方块中心的空白。 Since, if it is not fulfilled that there is a center, which one would remain blank?既然,如果不满足有一个中心,哪一个会保持空白? The case: size = 4 for example案例:size = 4 例如
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X
X X X X
X X X X
X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
If you want small Xs to have only one "X" border, you have to add the following condition to the if..else instruction:如果您希望小 X 只有一个“X”边框,则必须在 if..else 指令中添加以下条件:
for(int col=0; col<(size*3); col++) {
if ((((row/size!=1 && col/size!=1)) || (row/size==1 && col/size==1)) &&
(row%size==0 || col%size==0 || row%size==size-1 || col%size==size-1)) {
System.out.print("X ");
}else {
System.out.print(" ");
}
}
Where if row or column, are in module 0 or size-1 of size, is that they are on the edge of the small X.如果行或列位于模块 0 或大小为 1 的位置,则它们位于小 X 的边缘。
and the result is:结果是:
X X X X X X X X
X X X X
X X X X
X X X X X X X X
X X X X
X X
X X
X X X X
X X X X X X X X
X X X X
X X X X
X X X X X X X X
I do not know if it has been completely clear, it is my first post and I have tried to explain the best I know.我不知道它是否完全清楚,这是我的第一篇文章,我试图解释我所知道的最好的。
You could try something like this:你可以尝试这样的事情:
public static void main(String[] args) {
System.out.print("Enter the desired cube size: ");
int size = new Scanner(System.in).nextInt();
for (int row = 1; row <= size; row++) {
printRow(size, row);
}
}
private static void printRow(int size, int row) {
for (int i = 0; i < 3; ++i) {
printRow(size, row, i);
System.out.println();
}
}
private static void printRow(int size, int row, int subrow) {
for (int i = 1; i <= size; ++i) {
if (i == row || i == size-row+1) {
if (subrow == 1) {
System.out.print("X X");
} else {
System.out.print("XXX");
}
} else {
System.out.print(" ");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.