问题是一样的:如何编写一个程序来打印特定的 XX 网格? 我只想知道如何在代码中打印XX网格。
示例输出必须是这样的:
Type in the height: 5
Type in the width: 2
XX
XX
XX
XX
XX
<小时/>
import java.util Scanner;
public class Loops {
public static void main(String[]args){
Scanner input= new Scanner(System.in);
while( height <= 1 || width <= 1 ) {
System.out.println("Invalid height or width value");
System.out.print("Type in the height");
height= input.nextInt();
System.out.print("Type in the width:");
width = input.nextInt();
}
}
}
非常感谢任何帮助!谢谢。
最佳答案
当您知道高度
和宽度
时,您只需执行2个for-i循环
即可构建图案
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print("X");
}
System.out.println(); // jump line
}
关于java - 如何编写一个程序来打印特定的XX网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588136/