[英]Java Math convert to Integer
我正在尝试将float值转换为Integer,但无法将其转换。 它说找不到符号。 有什么方法可以做到这一点。
这是我的Java代码:
float upper = 999999;
float lower = 100000;
Integer ReceiptNo = 0;
Random rnd = new Random();
ReceiptNo = Math.round((Math.floor( (upper - lower + 1) * rnd() ) )) + lower;
这是我的vb代码:
Dim upper As Single = 999999 'Set the upper limit of random number.
Dim lower As Single = 100000
Dim ReceiptNo As Integer = 0
Randomize() 'Need to randomise the random number or else the number generated is always the same
ReceiptNo = CInt(Math.Floor((upper - lower + 1) * Rnd())) + lower
我试图重用Java中的VB代码。 谢谢。
rnd
是实例变量,而不是方法,因此您不能编写rnd()
。
你可以写 :
ReceiptNo = (int)(Math.round((Math.floor( (upper - lower + 1) * Math.random() ) )) + lower);
我不确定Rnd()
在VB中的作用,但是如果它在0和1之间产生一个随机双Math.random()
,那就是Math.random()
作用。
使用Math.random()
而不是rnd()
。 您需要将Math.round
的值(返回float
)转换为int
ReceiptNo = (int) (Math.round((Math.floor( (upper - lower + 1) * Math.random() ) )) + lower);
似乎应该是rnd.nextFloat()而不是rnd()
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html
Dim prng As New Random 'do NOT put this in a method
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim upper As Integer = 999999 + 1 'Set the upper limit of random number.
Dim lower As Integer = 100000
Dim ReceiptNo As Integer = prng.Next(lower, upper) 'the upper is exclusive
End Sub
rnd
这里是Random类的对象。 对象不能用作rnd()
。 正确的代码应为ReceiptNo = Math.round((Math.floor( (upper - lower + 1) * Math.random() ) )) + lower;
有关Java类和对象的更多信息, 请访问http://www.tutorialspoint.com/java/java_object_classes.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.