例如,假设用户输入了两个。在这种情况下,它会打印出一个正方形。
----- -----
| |
| |
----- -----
问题在于我无法根据用户输入获得正方形增长。
这是主要的课程
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;
}
}
答案 0 :(得分:1)
你甚至没有改变渲染方块的部分。
我认为您想要从constructSquare()
方法调用changeSize()
方法,或至少在再次打印之前调用constructSquare()
答案 1 :(得分:0)
将constructSquare()放在changeSize函数中。 所需的变更:
public SquareBuilder(int size)
{
changeSize( size);
// constructSquare();
}
public void changeSize(int size)
{
this.size = size;
constructSquare();
}