题目描述
编写一个程序,输入两个整数,作为栅格的高度和宽度,然后用“+”、“-”和“|”这三个字符来打印一个栅格。
输入
输入只有一行,包括两个整数,分别为栅格的高度和宽度。
输出
输出相应的栅格。
样例输入
3 2
样例输出
+-+-+ | | | +-+-+ | | | +-+-+ | | | +-+-+
【注意】:当输入的两个数只要其中一个为0时,输出就为空。
【AC代码】:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int n = sc.nextInt();
if (h == 0 || n == 0)
return;
for (int i = 0; i < 2 * h + 1; i++) {
if (i % 2 == 0)
for (int j = 0; j < 2 * n + 1; j++)
if (j % 2 == 0)
System.out.print("+");
else
System.out.print("-");
else
for (int j = 0; j < 2 * n + 1; j++)
if (j % 2 == 0)
System.out.print("|");
else
System.out.print(" ");
System.out.println();
}
}
}