1.简单写法
public class test01 {
public static void main(String args[]) {
//1.可能会出现第一位是0。
double a = Math.random() * 1000000;
int b = (int) a; //强制类型转换
System.out.println(b);
}
}
2.改善写法
public class test01 {
public static void main(String args[]) {
String s = ""; //s为空
double a = Math.random() * 10;
int b = (int) a;
s += b;
a = Math.random() * 10;
b = (int) a;
s += b;
a = Math.random() * 10;
b = (int) a;
s += b;
a = Math.random() * 10;
b = (int) a;
s += b;
a = Math.random() * 10;
b = (int) a;
s += b;
a = Math.random() * 10;
b = (int) a;
s += b;
System.out.println(s);
}
3.for循环
public class test01 {
public static void main(String args[]) {
String s = "";
for (int i = 0; i < 6; i++) {
int d = (int) (Math.random() * 10);
s = s + d;
}
System.out.println(s);
}
}