Math.random()函数在for循环中不起作用。

huangapple go评论138阅读模式
英文:

Math.random() function is not working in a for-loop

问题

在这段代码中,Math.random() 方法应该输出 1、2、3 或 4 中的一个数字,根据输出的数字,在相对位置绘制一个圆圈(RandomWalk Java 程序的一部分)。

但实际上,程序没有展示随机的圆点位置,它只打印向上移动的圆点(即只有当方向为 1 时表示向北移动),我对 Math.random() 方法是否存在问题感到困惑。

以下是代码:

int start_x = 200;
int start_y = 200;

double direction;

for (int i = 0; i < 20; i++)
{
    // 生成一个介于 1 和 4 之间的随机整数
    direction = (int) (Math.random() * 4) + 1;

    // 北方
    if (direction == 1)
    {
        g2.setColor(Color.BLUE);
        Ellipse2D.Double circle1 
            = new Ellipse2D.Double(start_x - 2, start_y - 12, 4, 4);
        g2.draw(circle1);

        start_y -= 10;
    }

    // 南方
    if (direction == 2)
    {
        g2.setColor(Color.BLUE);
        Ellipse2D.Double circle1 
            = new Ellipse2D.Double(start_x - 2, start_y + 8, 4, 4);
        g2.draw(circle1);

        start_y += 10;
    }

    // 东方
    if (direction == 3)
    {
        g2.setColor(Color.BLUE);
        Ellipse2D.Double circle1 
            = new Ellipse2D.Double(start_x + 8, start_y - 2, 4, 4);
        g2.draw(circle1);

        start_x += 10;
    }

    // 西方
    if (direction == 4)
    {
        g2.setColor(Color.BLUE);
        Ellipse2D.Double circle1 
            = new Ellipse2D.Double(start_x - 12, start_y - 2, 4, 4);
        g2.draw(circle1);

        start_x -= 10;
    }
}
英文:

In this piece of code, the Math.random() method is supposed to output either 1, 2, 3, or 4 and according to which number was outputted, draw a circle in a relative location (part of RandomWalk java program).

Instead of showing random dot locations, the program only prints dots going up (so only if(direction == 1) for North) was used, and I am confused if there is a problem with the Math.random() method that is making it do this.

Here is the code:

int start_x = 200;
int start_y = 200;
double direction;
for (int i = 0; i &lt; 20; i++)
{
// Generates random integer between 1 and 4
direction = (int) Math.random() * 4 + 1;
// North
if (direction == 1)
{
g2.setColor(Color.BLUE);
Ellipse2D.Double circle1 
= new Ellipse2D.Double(start_x - 2, start_y - 12, 4, 4);
g2.draw(circle1);
start_y -= 10;
}
// South
if (direction == 2)
{
g2.setColor(Color.BLUE);
Ellipse2D.Double circle1 
= new Ellipse2D.Double(start_x - 2, start_y + 8, 4, 4);
g2.draw(circle1);
start_y += 10;
}
// East
if (direction == 3)
{
g2.setColor(Color.BLUE);
Ellipse2D.Double circle1 
= new Ellipse2D.Double(start_x + 8, start_y - 2, 4, 4);
g2.draw(circle1);
start_x += 10;
}
if (direction == 4)
{
g2.setColor(Color.BLUE);
Ellipse2D.Double circle1 
= new Ellipse2D.Double(start_x - 12, start_y - 2, 4, 4);
g2.draw(circle1);
start_x -= 10;
}
}

答案1

得分: 4

这可能是因为您将值转换得太早了。
请尝试以下内容:

direction = (int) (Math.random() * 4) + 1;
英文:

That's probably because you cast your value to early.
Try the following:

direction = (int) (Math.random() * 4) + 1; 

huangapple
  • 本文由 发表于 2020年7月23日 18:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63051672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定