1

每当我尝试缩放形状时,什么都没有发生,然后控制台停止响应输入。这是设置比例命令:

case 7: do
                         {
                            System.out.println("\nEnter scale factor");
                            scale=input.nextDouble();
                         }
                         while(scale<0);
                         Shape.setScaleFactor(scale);
                         break;

这是比例形状命令

 case 4: for(i=0; i<s.length; i++)
                         {
                            if(s[i]!=null)
                            {
                              s[i].scaleShape();
                            }
                         }
                         break;

这是主要的方法变量:

int i=0;
        int m=0;
        double scale;
        boolean exit=false;
        Shape[] s = new Shape[10];

以下是shape类中的所有相关方法;

private static double scaleFactor;
public static double getScaleFactor()
    {

        return scaleFactor;
    }
    //Set ScaleFactor
    public static void setScaleFactor(double x)
    {

        scaleFactor=x;
    }

这是矩形子类中的 scaleshape 方法

private double base;
    private double height;   
@Override public void scaleShape()
    {
        base=base*getScaleFactor(); height=height*getScaleFactor();

    }

        public abstract void scaleShape();

这是 circle 类中的 set scale 方法:

private final double pi = Math.PI;
    private double radius;
 @Override public void scaleShape()
    {
         radius*=getScaleFactor();
    }
4

1 回答 1

1

我的猜测是你的问题在这里:

do
{
    System.out.println("\nEnter scale factor");
    scale = input.nextDouble();
}
while(scale < 0);

如果添加一些日志记录会发生什么?

do
{
    System.out.println("\nEnter scale factor");
    scale = input.nextDouble();
    System.out.println("Scale entered was: " + scale);
    System.out.println("Is scale < 0? " + (scale < 0));
}
while(scale < 0);

如果你没有得到你所期望的,那么阅读 a 可能是值得的,String只是为了看看你实际得到了什么输入。你总是可以读取一个字符串,记录它,然后用Double.valueOf(String)它来转换它。

Another possibility is that it's working, but you never reset scale and this is in some sort of loop, so it just continues scaling the shape forever but never stops to ask for input again.

于 2012-10-11T03:39:07.540 回答