Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 非常基本的GObject/GOval For循环_Java_Random_Colors_Acm Java Libraries - Fatal编程技术网

Java 非常基本的GObject/GOval For循环

Java 非常基本的GObject/GOval For循环,java,random,colors,acm-java-libraries,Java,Random,Colors,Acm Java Libraries,我目前正在参加AP Java编码课程,遇到了一个有趣的问题 我正在尝试使用GObjects/GOval在图形窗口中创建100个不同半径的随机圆和随机颜色。我已经尝试隔离这个问题,并且我确信for循环和GOval循环之间的通信是有问题的。我也尝试过几次从头开始重做这段代码,但我一直遇到同样的问题。具体来说,我的问题是,我的图形窗口只显示一个随机圆,而不是100。请,请帮忙。我的代码如下: 请注意,我选择了变量c来随机指定颜色。没有押韵或理由,我只需要使用一个随机值 import java.awt.

我目前正在参加AP Java编码课程,遇到了一个有趣的问题

我正在尝试使用GObjects/GOval在图形窗口中创建100个不同半径的随机圆和随机颜色。我已经尝试隔离这个问题,并且我确信for循环和GOval循环之间的通信是有问题的。我也尝试过几次从头开始重做这段代码,但我一直遇到同样的问题。具体来说,我的问题是,我的图形窗口只显示一个随机圆,而不是100。请,请帮忙。我的代码如下:

请注意,我选择了变量c来随机指定颜色。没有押韵或理由,我只需要使用一个随机值

import java.awt.Color;
import acm.graphics.GOval;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;

public class _100_Random_Circles extends GraphicsProgram 
{

    public _100_Random_Circles()
    {
      // Random Number Generator

      RandomGenerator rgen = new RandomGenerator();

      // Random X-coordinate.
      int x = rgen.nextInt(1, 500);
      // Random Y-coordinate.
      int y = rgen.nextInt(1, 500);
      // Random Circle width
      int c = rgen.nextInt(1, 100);
      // Random Circle height
      int d = rgen.nextInt(1, 100);

            for(int i = 0; i < 100; i++)
            {
            GOval circle = new GOval (x, y, c, d);
            add(circle);

            //Color the circles randomly

            if(c <= 10)
            {
            circle.setFilled(true);
            circle.setColor(Color.BLUE);
            }
            else if(c <= 20)
            {
            circle.setFilled(true);
            circle.setColor(Color.RED);
            }
            else if(c <= 30)
            {
            circle.setFilled(true);
            circle.setColor(Color.YELLOW);
            }
            else if(c <= 40)
            {
            circle.setFilled(true);
            circle.setColor(Color.GREEN);
            }
            else if(c <= 50)
            {
            circle.setFilled(true);
            circle.setColor(Color.ORANGE);
            }
            else if(c <= 60)
            {
            circle.setFilled(true);
            circle.setColor(Color.BLACK);
            }
            else if(c <= 70)
            {
            circle.setFilled(true);
            circle.setColor(Color.GRAY);
            }

            else if(c <= 80)
            {
            circle.setFilled(true);
            circle.setColor(Color.PINK);
            }

            else if(c <= 90)
            {
            circle.setFilled(true);
            circle.setColor(Color.MAGENTA);
            }

            else
            {
            circle.setFilled(true);
            circle.setColor(Color.WHITE);
            }
        }
    }
}

此代码在您的循环之外。你在同一个地方画了100个圆

正如我在评论中首先指出的那样


这是因为您的xycd定义在循环之外。然后,所有圆只有一个值

,这是因为xycd定义在循环之外。那么所有的圈只有一个值好吧,那么我应该把我的x/y/c/d变量放到for循环中?太好了!!非常感谢你!!
  // Random X-coordinate.
  int x = rgen.nextInt(1, 500);
  // Random Y-coordinate.
  int y = rgen.nextInt(1, 500);
  // Random Circle width
  int c = rgen.nextInt(1, 100);
  // Random Circle height
  int d = rgen.nextInt(1, 100);