如何使用Java中的Math.random()使随机数beetwen 1和90成为随机数?
是否正确:
Math.random()*90+1;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("first bound ");
int fbound = scanner.nextInt();
System.out.print("second bound: ");
int sbound = scanner.nextInt();
if (fbound != sbound) {
if (sbound > fbound) {
int range = (sbound - fbound) + 1;// inclusive 2 bounds
int ran = (int) (Math.random() * range) + fbound;// Math.random() method return double, but i do type casting
System.out.println(ran);
} else {
fbound = fbound + sbound;// change two bounds value without third variable
sbound = fbound - sbound;
fbound = fbound - sbound;
int range = (sbound - fbound) + 1;
int ran = (int) (Math.random() * range) + fbound;
System.out.println(ran);
}
} else {
System.out.println("This incoming bound is wrong!!!!!!!!");
}
main(args);
}
}
我为动态界线写书,同时您找到了不同的方式!
示例 ---> Math.random() explanation,https://programmerfriend.com/java-random-numbers/