如何在java中打印数字模式方块
我正在尝试制作一个输出以下内容的程序:如何在java中打印数字模式方块,java,Java,我正在尝试制作一个输出以下内容的程序: 12345 54321 12345 54321 12345 到目前为止,我已经做了: public class GotThatBoom { public static void main(String[] args) { int row, col, num; for (row = 1; row <= 5; row++) { num = row; for (col
12345
54321
12345
54321
12345
到目前为止,我已经做了:
public class GotThatBoom {
public static void main(String[] args) {
int row, col, num;
for (row = 1; row <= 5; row++) {
num = row;
for (col = 1; col <= 5; col++) {
System.out.printf("%d ", num);
num++;
if ()
}
System.out.printf("\n");
}
}
}
您能帮助我吗,谢谢。当您的行号为偶数时,您希望从5打印到1,当行号为奇数时,您希望从1打印到5。 因此:
公共类{
公共静态void main(字符串[]args){
int row,col;
对于(row=1;row当您的行为奇数时,从1到5,当为奇数时,从5到1:
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 5; col++) {
if (row % 2 != 0) {
System.out.printf("%d ", col);
} else {
System.out.printf("%d ", 6 - col);
}
}
System.out.printf("\n");
}
}
欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南,正如您创建此帐户时所建议的。关于主题和如何提问,请在此处应用。StackOverflow不是一项设计、编码、研究、调试或教程服务。根据您的问题说明,是什么阻止您这样做:System.out.println(“12345”);System.out.println(“54321”)
…当遇到不清楚的问题时,您会看到问题吗?您提供了您的尝试,但没有提供有效的问题。请注意:如果控制台使用的字体不是sansserif字体,您将不会收到一个方框。非常感谢您的帮助@tsamridh86如果您认为答案正确,请将其标记为已接受:)
public class GotThatBoom {
public static void main(String[] args) {
int row, col;
for (row = 1; row <= 5; row++) {
for (col = 1; col <= 5; col++) {
if( row %2 == 0)
System.out.printf("%d ", col);
else
System.out.printf("%d", 6 - col );
}
System.out.printf("\n");
}
}
}
public static void main(String[] args) {
for (int row = 1; row <= 5; row++) {
for (int col = 1; col <= 5; col++) {
if (row % 2 != 0) {
System.out.printf("%d ", col);
} else {
System.out.printf("%d ", 6 - col);
}
}
System.out.printf("\n");
}
}
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5