java - 如何编写一个程序来打印特定的XX网格?

标签 java loops

问题是一样的:如何编写一个程序来打印特定的 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/

相关文章:

java - Voldemort 中任意 HashMap 的 JSON 序列化程序

powershell - 想将字节写入多个二进制文件

php - 仅当数据库中不存在数据时才插入

java - 使用循环检查现有文件

java - SSL协议(protocol)可以保护Web Service吗?

java - 无法找到 .myeclipse.properties 文件

java - 如何在 spring-boot 中禁用静态内容句柄?

java - 交替使用 OutputStream、Output Stream Writer 和 BufferedWriter

c - 如何提示用户输入一定数量以内的整数

c++ - "for each"是否保证可以与更改数组一起使用?