简体   繁体   English

我想制作一个随机图像生成器,但是它不起作用

[英]I wanted to make an random image generator but it does not work

So i have made this program and i wanted it t create a 8 by 8 sized image with only Black and White but it does only display a white color instead of randomly spread black and white. 所以我做了这个程序,我希望它不能创建只有黑白的8 x 8尺寸的图像,但是它只显示白色,而不是随机分布黑白。 Here is my code. 这是我的代码。 If someone can help that'd be great :D 如果有人可以帮助那会很棒:D

package de.gamingengine.main;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String args[])throws IOException {

        int width = 8;
        int height = 8;
        Color c = null;

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        File f = null;

        for(int y = 0; y < height; y++) {

            for(int x = 0; x < width; x++) {

                int blackorwhite = (int)Math.random()*10;

                if(blackorwhite >= 5) {

                    c = Color.BLACK;

                } else if(blackorwhite < 5){

                    c = Color.WHITE;

                }

                img.setRGB(x, y, c.getRGB());

            }

        }

        try {

            f = new File("C:\\Users\\Linus\\Desktop\\Out.png");
            ImageIO.write(img, "png", f);

        } catch (IOException e) {

            System.out.println("Error: " + e);

        }

    }

}

The problem is the precedence of operators. 问题是运算符的优先级。 Here : 这里 :

(int) Math.random() * 10

First you will cast the result of Math.random() to int . 首先,您将Math.random()的结果转换为int Since this method returns values from [0,1) , you cast it to int so it is always 0 and then you multiply it by 10 but it is still 0 in the end. 由于此方法从[0,1)返回值,因此将其[0,1)int因此始终为0,然后将其乘以10,但最后仍为0。

Change your code to : 将您的代码更改为:

int blackorwhite = (int) (Math.random() * 10);

Cast to int after multiply: 乘法后转换为整数:

(int)(Math.random()*11)

See Random with range 请参阅范围随机

  int range = (max - min) + 1; return (int)(Math.random() * range) + min; 

While all the answers mentioning operator precedence and the that the cast must be on the complete expression are correct, I like to point out that there is a better option for this specific case: 尽管所有提到运算符优先级和强制转换必须在完整表达式上的答案都是正确的,但我想指出,对于这种特定情况,有一个更好的选择:

Use Random.nextInt(int bound) instead. 请改用Random.nextInt(int bound)

It returns a pseudo-random number in the range [0...bound> . 它返回[0...bound>范围内的伪随机数。 This method is more efficient and mathematically more correct due to less bias (but I doubt this makes much difference for your use case). 由于偏差较小,因此该方法更有效,并且在数学上更正确 (但我怀疑这对您的用例会有很大影响)。

Random random = new Random();

for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        int blackorwhite = random.nextInt(10); // Look, no cast

        // Rest of the code as-is
        ...
    }
}

PS: I think your code would be even clearer if you just used nextInt(2) or nextBoolean() . PS:我认为,如果您仅使用nextInt(2)nextBoolean()您的代码将更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM