【问题标题】:In main method call scale on rect with value 10 and print the rectangle area afterwards在值为 10 的 rect 上调用 scale 的 main 方法,然后打印矩形区域
【发布时间】:2016-06-17 00:28:05
【问题描述】:

我正在使用 java AIDE 应用程序,并被要求完成我的问题中所述的任务。

每次我运行我的代码时,应用都会告诉我“程序应该在缩放后打印矩形区域。”

我不确定我做错了什么,请参阅下面的代码: ....p.s 我仍然不能 100% 确定缩放意味着什么,如果有人能解释一下,我将不胜感激。谢谢

public class Main
{
public static void main(String[] args)
{

Rectangle rect = new Rectangle(100, 200);
        System.out.println(rect.getWidth());
        System.out.println(rect.getHeight());


    rect.setWidth(10);
    rect.setHeight(20);
    rect.scale(10);
System.out.println(rect.getArea());


}
}

class Rectangle
{
int width;
int height;
int factor;


Rectangle(int width, int height) 
    {
    this.width = width;
    this.height = height;
    }

void setHeight(int height)
{
    this.height = height;
}

void setWidth(int width)
{
    this.width = width;
}

int getWidth()
{
    return this.width;
}

int getHeight()
{
    return this.height;
}

void scale(int factor)
{
width = width * factor;
height = height * factor;
}

int getArea()
{
return this.width * this.height;
}

【问题讨论】:

    标签: java scale


    【解决方案1】:

    您的代码似乎对我有用,下面的输出。您确定您正在从您的 IDE 运行已编译的 Main 类吗?

    100
    200
    20000
    

    【讨论】:

      【解决方案2】:

      我不确定你的java AIDE app,但我发现你已经用(100, 200) 初始化了rect,然后你将它重置为(10, 20),所以你的应用程序是否有可能认为它是scale of 0.1?我猜,也许你应该在更改之后打印该区域。

      【讨论】:

        【解决方案3】:

        你只需要写rect.scale(10); System.out.println(rect.getArea()); 在新行就像这个

            public class Main
        {
        public static void main(String[] args)
        {
        
        Rectangle rect = new Rectangle(100, 200);
                System.out.println(rect.getWidth());
                System.out.println(rect.getHeight());
        
        
            rect.setWidth(10);
            rect.setHeight(20);
        System.out.println(rect.getArea());
        
            rect.scale(10);
        System.out.println(rect.getArea());
        
        
        }
        }
        

        ,在哪里

        rect.scale(10);
            System.out.println(rect.getArea());
        

        是分开的。

        【讨论】:

          猜你喜欢
          • 2011-10-22
          • 2017-08-05
          • 1970-01-01
          • 2014-10-29
          • 1970-01-01
          • 2021-06-11
          • 1970-01-01
          • 2020-11-03
          • 1970-01-01
          相关资源
          最近更新 更多