|
How to get a random integer value in midlets at runtime |
|
|
package java.util provides Random Class. Random Class has method nextInt(n).
This method will return any number between 0 - n(int value). This method
will successfully return integer value only on devices having configuration
greater than CLDC 1.0.
Below is an example of Random class that return any number between 0-10.
import javax.microedition.midlet.*;
import java.util.*;
public class testRandom extends MIDlet
{
public static void testRandom()
{
}
public void startApp()
{
Random r = new Random();
int i = r.nextInt(10);
System.out.println( i);
}
public void pauseApp()
{}
public void destroyApp(boolean destroy) {}
}
|
For devices with CLDC 1.0 below is the method to implement above functionality.
public void startApp()
{
Random r = new Random();
int i = r.nextInt() % <10>;
System.out.println( i);
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.