gpt4 book ai didi

java - 修复根据用户输入增大正方形的问题

转载 作者:行者123 更新时间:2023-12-01 14:37:45 25 4
gpt4 key购买 nike

我写了两个类。主类运行该程序。它要求用户输入。根据用户输入,第二类将打印出一个正方形。每个方 block 都会打印出 ---- 对于每个数字和 |对于墙壁的每个数字。

例如,假设用户输入了两个。在这种情况下,它将打印出一个正方形。

----- -----
| |
| |
----- -----

问题是我无法根据用户输入获得增长的平方。

这是主类

import java.util.Scanner;

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

SquareBuilder square = new SquareBuilder(scan.nextInt());

System.out.print(square.toString());
square.changeSize(scan.nextInt());
System.out.print(square.toString());
square.changeSize(scan.nextInt());
System.out.print(square.toString());
square.changeSize(scan.nextInt());
System.out.print(square.toString());
square.changeSize(scan.nextInt());
System.out.print(square.toString());
}
}

public class SquareBuilder
{
final private int LENGTH_RATIO = 4;
final private String CEILING_FLOOR = "----";
private int size;
private int spaces;
private String square;

public SquareBuilder(int size)
{
this.size = size;
constructSquare();
}

private void spaces()
{
spaces = LENGTH_RATIO * size - 2;
}

private void ceilingAndFloor()
{
square += "\n";
for (int i = 0; i < size; ++i)
{
square += CEILING_FLOOR;
}
}

private void walls()
{
for (int i = 0; i < size; ++i )
{
square +="\n|";

for (int j = 0; j < spaces; ++j)
{
square +=" ";
}

square +="|";
}
}

private void constructSquare()
{
spaces ();
square = "";

ceilingAndFloor();
walls();
ceilingAndFloor();

}

public int area()
{
return size * size;
}

public void changeSize(int size)
{
this.size = size;
}

public String toString()
{
String retVal = square;
retVal += "\nSize: " + size + " Area: " + area();

return retVal;
}
}

最佳答案

您甚至没有更改正方形的渲染部分。

我认为您想从 changeSize() 方法调用 constructSquare() 方法,或者至少在之前调用 constructSquare()你再打印一次

关于java - 修复根据用户输入增大正方形的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16323538/

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