Math.random()只有两个数字

Math.random()只有两个数字,math,random,numbers,Math,Random,Numbers,我想用Math.random()只创建数字2和4。 我该怎么做 感谢您的支持inttwoorfore=Math.random()

我想用
Math.random()
只创建数字2和4。 我该怎么做


感谢您的支持

inttwoorfore=Math.random()<0.5?2 : 4;

“?”和“:”被称为三元运算符。它们是if-else的替代品。前面的代码在if-else中应该是这样的


if(Math.random())是所谓的三元运算符。这是一样的,就像写:int a=Math.random();if(a)在这里为这个运算符确定:谢谢!你太棒了!你没有指定语言。这是Java吗?JavaScript?