设置java不工作的方法

设置java不工作的方法,java,class,methods,Java,Class,Methods,因此,对于我的作业,它需要如下所示: 我面临的问题是setIndent不是设置缩进,而是当我主要更改intindent=20时它将向框中添加缩进。我也不明白为什么Rectangle@6bdf28bb出现在我的代码中 这是我的作业代码 客户 等级 //使用矩形类进行测试 公共类矩形{ 公共双倍长度; 公共双宽度; 公共字符填充=“”; 公共字符笔=“*”; 公共整数缩进; //设置变量 公共无效设置长度(双长度){ 如果(len您想将矩形的打印过程移到构造函数之外,否则在使用新矩形(…)之后,它总

因此,对于我的作业,它需要如下所示:

我面临的问题是
setIndent
不是设置缩进,而是当我主要更改
intindent=20时它将向框中添加缩进。我也不明白为什么
Rectangle@6bdf28bb
出现在我的代码中

这是我的作业代码

客户 等级
//使用矩形类进行测试
公共类矩形{
公共双倍长度;
公共双宽度;
公共字符填充=“”;
公共字符笔=“*”;
公共整数缩进;
//设置变量
公共无效设置长度(双长度){

如果(len您想将
矩形的打印过程移到构造函数之外,否则在使用
新矩形(…)
之后,它总是会立即打印,然后才能使用
矩形#setIndent(int)

相反,您应该使用构造函数设置
矩形
字段的值,然后使用单独的方法打印
矩形

例如,用于定义具有自定义宽度、长度、画笔和填充的特定
矩形的构造函数:

public Rectangle(double l, double w, char p, char f) {
    this.length = l;
    this.width = w;
    this.pen = p;
    this.fill = f;
}
这会将矩形实例的字段设置为使用
新矩形(…)
时解析为参数的值(注意,您可能还需要重做其他构造函数以符合此要求)

要使其可视化,您可以尝试将以下代码添加到
Rectangle
类中

@Override
public String toString() {
    return getClass().getSimpleName() + "[w: " + width + "; l: " + length + 
            "; p: " + pen + "; f: " + fill + "; indent: " + indent + "]";
}
然后在
矩形客户端中使用以下命令

Rectangle box1 = new Rectangle(4, 5, '+', 'X');
System.out.println(box1);
box1.setIndent(50);
System.out.println(box1);
应该打印出来

Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 0]
Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 50]
因为我们从构造函数中删除了打印框的逻辑,所以我们应该将它添加到其他地方

public void printRectangle() {
    int count = 0;
    String indents = "";
    String topBottom = "";
    String middle = "";
    String line = "";
    // Creates the indent string
    while (count < indent) {
        indents += " ";
        count++;
    }
    // Top boarder and bottom one
    count = 0;
    while (count < this.width) {
        topBottom += this.pen;
        count++;
    }
    // Fill inside square
    this.width = this.width - 2;
    count = 0;
    while (count < this.width) {
        middle += this.fill;
        count++;
    }
    // Prints square
    line = indents + this.pen + middle + this.pen;
    topBottom = indents + topBottom;
    count = 0;
    while (count < this.length) {
        if (count == 0 || count == this.length - 1) {
            System.out.println(topBottom);
            count++;
        } else {
            System.out.println(line);
            count++;
        }
    }
}

如果选择重写
#toString()
,则不应在逻辑中使用
System.out.println
,而是构建一个字符串,并在
#toString()末尾返回该字符串
逻辑。你可以看看这个。

我不明白为什么Rectangle@6bdf28bb请参阅将打印代码移动到构造函数之外的方法,并在设置variables@ScaryWombat谢谢你,我能移除Rectangle@6bdf28bb.如果您正在这里讨论这个问题。矩形框3=新矩形ngle(11,20,“$”,“o”);box3.setIndent(20);对于方法,我的导师就是这样做的,我们必须进行匹配。除非他输入错误?不,问题是当您调用
新矩形(…)
它已经在打印输出了。可怕的袋熊指的是,在设置缩进值之前,您实际上要将方框打印到控制台上。每当您调用
新矩形(…)
,您将框的可视表示形式打印到控制台,因为打印的逻辑在矩形构造函数内部。因此,将打印任何内容的逻辑移动到矩形构造函数外部的方法,然后在使用
#setIndent
后从矩形客户端调用它。您刚才解释了toString()方法比我的导师为我做的要好。非常感谢您解释
toString()
很难…有这么多用例和其他分支。多年后我仍在学习新东西。这是一个很好的实用解释。
Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 0]
Rectangle[w: 5.0; l: 4.0; p: +; f: X; indent: 50]
public void printRectangle() {
    int count = 0;
    String indents = "";
    String topBottom = "";
    String middle = "";
    String line = "";
    // Creates the indent string
    while (count < indent) {
        indents += " ";
        count++;
    }
    // Top boarder and bottom one
    count = 0;
    while (count < this.width) {
        topBottom += this.pen;
        count++;
    }
    // Fill inside square
    this.width = this.width - 2;
    count = 0;
    while (count < this.width) {
        middle += this.fill;
        count++;
    }
    // Prints square
    line = indents + this.pen + middle + this.pen;
    topBottom = indents + topBottom;
    count = 0;
    while (count < this.length) {
        if (count == 0 || count == this.length - 1) {
            System.out.println(topBottom);
            count++;
        } else {
            System.out.println(line);
            count++;
        }
    }
}
@Override
public String toString() {
    // Logic from #printRectangle() here
}