十二、打印1-10之间的随机数 (lang包底下的math类)
打印1-10之间的随机数 (lang包底下的math类)
1.public class Helloworld1 { //lang包里面的math类的random()方法
public static void main(String args[]) {
System.out.println(Math.floor(Math.random()*10)+1); //也可以(int)(Math.random()*10)+1或Math.ceil(Math.random()*10)
}
}
2.import java.util.*; //util包里面的Random类的nextInt()方法
public class Helloworld1 {
public static void main(String args[]) {
System.out.println(new Random().nextInt(10)+1);
}
}
浙公网安备 33010602011771号