gpt4 book ai didi

java - 如何在java中绘制正方形?

转载 作者:行者123 更新时间:2023-11-30 11:08:59 34 4
gpt4 key购买 nike

这是我必须做的:

You are to write a program that draws a square composed of # signs onto a grid. The user will enter the size (in the length of the sides), the x-coordinate, and the y-coordinate of the bottom-left corner of the square on the grid as command-line parameters to program, in that order.

In other words, the three values will be stored in the String array args at the start of the program, as the values args[0],
args[1], and args[2]
. You can use the command Integer.parseInt() to convert the String into an int. Assume that the input parameters are valid ints.

例如,在 DrJava 的交互面板中:

在 2 5 1 1 上运行问题表示我们要绘制一个边长为 5 的正方形,其左下角位于位置 (1, 1)。此问题中的所有距离均以字符数表示。默认情况下,程序应该打印将正方形放到 15x15 的网格上,但如果正方形放不下,则必须相应地扩展网格。

我尝试分三步构建我的程序。

  1. 编写代码以正确显示轴。

  2. 构建一个正方形始终适合 15x15 网格的解决方案。

  3. 如何扩展程序以使其适用于较大的正方形或移动太多而无法适应 15x15 网格的正方形。

到目前为止,这是我的代码:

import java.util.Scanner;

public class drawS {
public static void main(String[] args) {
Scanner ask = new Scanner(System.in);

int length = ask.nextInt();
int i;
int r = 15;
int xmax = 15;
int ymax = 15;

drowLine(length);

System.out.println();

if (i == 0 && j == 0) {
System.out.print("+");
}
if (i == 0 && j <= ymax) {
System.out.print("|");
}
if (j == ymax) {
System.out.print("^");
}
for (int j = ymax; j >= 0; j--) {
for (int i = 0; i <= xmax; i++) {
for (i = 0; i < length - 2; i++) {
drowEmptyLine(length);
System.out.println();
}
drowLine(length);
}

public static void drowLine ( int n){
for (int i = 0; i < n; i++) {
System.out.print("");
}
}

public static void drowEmptyLine ( int n){
System.out.print("");
for (int i = 0; i < n - 2; i++) {
System.out.print("# ");
}
System.out.print('#');
}
}
}
}

最佳答案

显然是在挖坟:-)

首先,我们必须获取 CLI 上提供的参数,并将它们转换为净化后的整数。

    public static void main(String[] args) {
try {
int size = Integer.parseInt(args[0]);
int x = Integer.parseInt(args[1]);
int y = Integer.parseInt(args[2]);

if (size < 1 || size > 30) {
System.out.println("Setting size to 5");
size = 5;
}

if (x < 0 || x > 30) {
System.out.println("Setting x to 2");
x = 2;
}

if (y < 0 || y > 30) {
System.out.println("Setting y to 2");
y = 2;
}

int height = Math.max(y + size + 1, 15);
int width = Math.max(x + size + 1, 15);
drawSquare(height, width, size, x, y);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

要绘制正方形,我们必须从上到下,因为我们使用的是 CLI。或者,可以填充一个大的 char[][] 并在最后打印出来——从上到下 :-)。部分代码中的整个“-1”基本上是由于数组等是基于 0 的事实。

    static void drawSquare(int height, int width, int size, int x, int y) {
for (int line = height -1 ; line >= 0; line--) {
drawline(line, size, x, y, width);
}
}

行可以分为空行、代表正方形顶部和底部的行以及代表正方形内容的行。

    static void drawline(int line, int size, int x, int y, int width) {
if (line > y + size - 1 || line < y) {
drawEmptyLine(width);
} else if (line == y + size - 1 || line == y) {
drawBorder(x, size, width);
} else {
drawContent(x, size, width);
}
}

空行很简单:

    static void drawEmptyLine(int width) {
draw(emptyLine(width));
}

static char[] emptyLine(int width) {
char[] dots = new char[width];
Arrays.fill(dots, 0, width, '.');
return dots;
}

static void draw(char[] chars) {
StringBuffer sb = new StringBuffer(chars.length);
sb.append(chars);
System.out.println(sb);
}

边框和内容行只是用一些散列替换空行中的点:

    static void drawContent(int x, int size, int width) {
char[] dots = emptyLine(width);
dots[x] = '#';
dots[x + size - 1] = '#';
draw(dots);
}

static void drawBorder(int x, int size, int width) {
char[] dots = emptyLine(width);
Arrays.fill(dots, x, x + size, '#');
draw(dots);
}

就是这样。

关于java - 如何在java中绘制正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311027/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com