javaMath类随机数math.random()

来源:互联网 发布:笑气在淘宝上叫什么 编辑:程序博客网 时间:2024/05/21 14:00

首先我们看下Math.random()的取值范围,0=<Math.random()<1;切记不能取到1,

接下来写个a到b的随机数方法;


package com.Math随机数;


import java.util.Scanner;


public class Mathrandom {
// static void get_namber(int low,int hig){
// (hig-low+1)表示地位之间的数,相乘得到之间的随机数,加上低位数,再转型就是我们想要的随机数
// int random=(int)(Math.random()*(hig-low+1)+low);
// System.out.println(random);
//
// }
static int count=0;
public static int get_namber(int low ,int hig){
return (int)(Math.random()*(hig-low+1)+low);
}
public static void run_get_namber(){

Scanner sc=new Scanner(System.in);
System.out.println("请输入最小的数");
int low=sc.nextInt();

Scanner sc1=new Scanner(System.in);
System.out.println("请输入最大 的数");
int hig=sc1.nextInt();

System.out.println(get_namber(low,hig));
}
public static void main(String[] args) {
while(true){
run_get_namber();
count++;
// 执行3次后停止
System.out.println(count);
if(count>=3){
System.exit(0);
}
}

}
}

阅读全文
0 0