Return True or False Randomly

前端 未结 7 1200
傲寒
傲寒 2020-12-23 10:59

I need to create a Java method to return true or false randomly. How can I do this?

7条回答
  •  隐瞒了意图╮
    2020-12-23 11:36

    You can do as following code,

    public class RandomBoolean {
        Random random = new Random();
        public boolean getBoolean() {
            return random.nextBoolean();
        }
        public static void main(String[] args) {
            RandomBoolean randomBoolean = new RandomBoolean();
            for (int i = 0; i < 10; i++) {
                System.out.println(randomBoolean.getBoolean());
            }
        }
    }
    

    Hope this would help you, Thanks.

提交回复
热议问题