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/07/02 08:30:40
![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随机数 为什么第二个打印在循环里不变 打出来的永远是一个](/uploads/image/z/7179481-1-1.jpg?t=Math.random%28%29%E9%97%AE%E9%A2%98while%28%28inputx%3Dbr.nextLine%28%29%29%21%3Dnull%29%7BSystem.out.print%28Math.round%28Math.random%28%29%2A14%29%2B1%29%3BSystem.out.print%28%28int%29Math.random%28%29%2A14%2B1%29%3B%7D%E9%9A%8F%E6%9C%BA%E4%BA%A7%E7%94%9F%E4%B8%80%E4%B8%AA1-15%E9%9A%8F%E6%9C%BA%E6%95%B0+%E4%B8%BA%E4%BB%80%E4%B9%88%E7%AC%AC%E4%BA%8C%E4%B8%AA%E6%89%93%E5%8D%B0%E5%9C%A8%E5%BE%AA%E7%8E%AF%E9%87%8C%E4%B8%8D%E5%8F%98+%E6%89%93%E5%87%BA%E6%9D%A5%E7%9A%84%E6%B0%B8%E8%BF%9C%E6%98%AF%E4%B8%80%E4%B8%AA)
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