[英]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.