Math.random()问题while((inputx=br.nextLine())!=null){System.out.print(Math.round(Math.random()*14)+1);System.out.print((int)Math.random()*14+1);}随机产生一个1-15随机数 为什么第二个打印在循环里不变 打出来的永远是一个
来源:学生作业帮助网 编辑:作业帮 时间:2024/06/28 03:20:00
Math.random()问题while((inputx=br.nextLine())!=null){System.out.print(Math.round(Math.random()*14)+1);System.out.print((int)Math.random()*14+1);}随机产生一个1-15随机数 为什么第二个打印在循环里不变 打出来的永远是一个
Math.random()问题
while((inputx=br.nextLine())!=null)
{
System.out.print(Math.round(Math.random()*14)+1);
System.out.print((int)Math.random()*14+1);
}
随机产生一个1-15随机数 为什么第二个打印在循环里不变 打出来的永远是一个随机数 第一个就会变化呢
Math.random()问题while((inputx=br.nextLine())!=null){System.out.print(Math.round(Math.random()*14)+1);System.out.print((int)Math.random()*14+1);}随机产生一个1-15随机数 为什么第二个打印在循环里不变 打出来的永远是一个
第二个修改为:System.out.print((int) (Math.random()*14) + 1);
要先计算Math.random()*14的值再转换为int型数据,强制转换类型的优先级比*要高
Math.random()返回带正号的 double 值,该值大于等于 0.0 且小于 1.0,如果先强制转换类型为int的话,返回为0,那么第二个始终返回为1