【问题标题】:how do I prevent my variables going on forever?如何防止我的变量永远持续下去?
【发布时间】:2013-02-19 08:01:53
【问题描述】:

Dr.Java 不会运行我的程序,因为我的变量会永远存在,我不知道如何防止这种情况发生。如果你能告诉我如何解决这个问题,那就太好了。我是初学者,所以这对我来说都是陌生的。

这是课程的代码:

import java.awt.Color;
class PaintablePicture extends Picture {
    public PaintablePicture(String fileName) {
        super(fileName);
    }

    public void purpleSplotch(int x, int y) {
        int x1 = 0;
        int y1 = 1;
        while (x1 < x * 2)
            while (y1 < y * 3)

            {
                Color purple = new Color(175, 0, 175);
                Pixel pixRef;
                pixRef = this.getPixel(x, y);
                pixRef.setColor(purple);

            }
        return;

    }
}

然后这就是我运行它的方法(忽略其他类的所有艺术乌龟材料,一切正常,直到我开始绘制可绘制图片。

public class GraffitiApp
{
  public static void main(String[] a)
{

FileChooser.pickMediaPath();
PaintablePicture pRef;
pRef = new PaintablePicture(FileChooser.pickAFile());
pRef.purpleSplotch(14,14); 
pRef.purpleSplotch(17,20);

ArtisticTurtle tRef = new ArtisticTurtle(pRef);
tRef.pentagon(20);
tRef.spiral(50);
tRef.penUp();
tRef.forward(-50);
tRef.turn(90);
tRef.penDown();
tRef.letterK(50);
tRef.penUp();
tRef.turn(-130);
tRef.forward(100);
tRef.turn(90);
tRef.forward(140);
tRef.penDown();
tRef.letterK(30);
tRef.penUp();
tRef.moveTo(486, 60);
tRef.penDown();
tRef.star(30);
tRef.penUp();
tRef.moveTo(159,122);
tRef.turn(30);
tRef.penDown();
tRef.star(60);
tRef.penUp();
tRef.moveTo(330,103);
tRef.turn(-67);
tRef.penDown();
tRef.pentagon(40);
tRef.penUp();
tRef.moveTo(471,158);
tRef.penDown();
tRef.spiral(35);

pRef.explore();

} }

【问题讨论】:

  • x1y1 值在 while 循环中从不使用/递增
  • 在任何地方发布之前格式化您的代码。 Eclipse -> Ctrl+Shift+F,NetBeans -> Alt+Shift+F

标签: java variables loops while-loop


【解决方案1】:

我觉得应该是的

class PaintablePicture extends Picture {
    public PaintablePicture(String fileName) {
        super(fileName);
    }

    public void purpleSplotch(int x, int y) {
        int x1 = 0;
        int y1 = 1;
        while (x1 < x * 2)
            while (y1 < y * 3)

            {
                Color purple = new Color(175, 0, 175);
                Pixel pixRef;
                // get pixels (x1, y1) instead of (x, y) there is use to set the color of the same pixel(x,y) in a loop
                pixRef = this.getPixel(x1, y1);
                pixRef.setColor(purple);
                // increment y1
                y1++;

            }
        // increment x1
        x1++;
    }
}

在您的 while 循环中,循环变量 x1y1 永远不会更改,这意味着它们始终是 01,它们将永远满足条件,这就是您的程序永远运行的原因。

在任何带有临时变量的循环中,您都需要更改循环变量的值,以便在某个阶段满足条件。

这里我们将增加循环内的x1y1变量,x1循环中的retuen语句将导致循环在第一次执行后退出,它必须被删除。

此外,您总是得到像素 (x,y),x 和 y 的值在整个循环中保持不变,我猜您需要的是像素 (x1, y1)

【讨论】:

    【解决方案2】:

    您的问题是您没有增加 while 循环中用作索引的变量,因此循环无限期地运行。增加两个变量(x1y1)以确保您将在某个时候退出这两个 while 循环。目前,x1 永远不会超过 xy1 永远不会超过 y

    while(x1 < x*2) {
        while(y1 < y*3) {
             Color purple = new Color(175, 0, 175);
             Pixel pixRef;
             pixRef= this.getPixel(x,y);
             pixRef.setColor(purple);
             //increment y1 here
             y1++;
        }
        //increment x1 here
        x1++;
    }
    

    【讨论】:

    • 颜色不会变成紫色,我的图片上没有显示,
    【解决方案3】:

    当然会一直运行

    while(x1 < x*2)
    

    在调用方法时 x 的值大于 0

    pRef.purpleSplotch(14,14);
    

    y1 的 while 循环也是如此。

    您可能需要在 PurpleSplotch 方法中增加 x1 和 y1。我不知道它必须做什么,但条件将永远为真,因此无限循环。

    【讨论】:

    • 您好,我纠正了它,但现在我的污点不会出现,我很难过
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2013-05-06
    • 2018-10-09
    相关资源
    最近更新 更多