Math
public class MathDemo {
public static void main(String[] args) {
double d = Math.ceil(16.34);//ceil返回大于指定数据的最小整数。
double d1 = Math.floor(12.34);//floor返回小于指定数据的最大整数。
long l = Math.round(12.54);//四舍五入
sop("d="+d);
sop("d1="+d1);
sop("l="+l);
double d2 = Math.pow(2,3);
sop("d2="+d2);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出
d=17.0
d1=12.0
l=13
d2=8.0
Random
public class RandomDemo {
public static void main(String[] args) {
for (int x=0;x<10;x++)
{
double d = Math.random();
sop(d);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出 [0,1)
0.44362060760626965
0.786069912604457
0.7456806158981336
0.41908432061119616
0.5847469756217509
0.4770086241970416
0.3620804547796914
0.7859736838982817
0.0018884731926712695
0.01182912216363452
public class RandomDemo {
public static void main(String[] args) {
for (int x=0;x<10;x++)
{
int d = (int)(Math.random()*10 + 1);
sop(d);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出 [1,10]
1
10
5
6
10
2
3
10
4
10
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
for (int x=0;x<10;x++)
{
int d = r.nextInt(10)+1;
sop(d);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//输出 [1,10]
1
2
5
3
10
3
8
4
2
2
网友评论