问答题Math.random()方法用来实现什么功能?下面的语句起到什么作用? (int)(Math.random()*6)+

题目
问答题
Math.random()方法用来实现什么功能?下面的语句起到什么作用? (int)(Math.random()*6)+

相似考题
更多“问答题Math.random()方法用来实现什么功能?下面的语句起到什么作用? (int)(Math.random()*6)+”相关问题
  • 第1题:

    classSock{Stringsize;Stringcolor;publicbooleanequals(Objecto){Socks=(Sock)o;returncolor.equals(s.color);}//insertcodehere}哪两个满足hashCode的约定?()

    A.publicinthashCode(){return343;}

    B.publicinthashCode(){returnsize.hashCode();}

    C.publicinthashCode(){returncolor.hashCode();}

    D.publicinthashCode(){return(int)(Math.random()*1000);


    参考答案:B, C

  • 第2题:

    下面程序段的输出结果是 public class Test{ public static void main(Stringargs[]){ int x,y; x=(int)Math.sqrt(5)/2+(int)Math.random()*5/2; y=(int)Math.sqrt(3)/2+(ht)Math.random()*3/2; if(x>y) System.out.println(”x>y”); else if(x=y) System.out.println(”x=y”); else System.out.Println(”x<y”); } }

    A.x>y

    B.x=y

    C.x<y

    D.编译错误


    正确答案:A
    解析:本题考查Java语言中if-else分支结构和几个标准函数语句的用法。Java语言的控制结构是考试重点,考生应该多读相关程序。这里重点理解程序中的两个赋值语句,赋值号右边的两个表达式分别求两个标准函数的值,再进行整除,判断出x与y的大小。Math.sqrt计算一个数的平方根,Math.random()输出[0,1)之间的随机数,本题中经过两条赋值语句后,x=1,y=O。进入分支结构后,满足if条件执行相应System.out.println(”x>y”);操作。

  • 第3题:

    在程序中,DataPool是一个数据池,能存放一个血型数据,线程a和线程b负责向其中存放数据,一次只能有一个线程向其中存放数据,数据放入DataPool以后,该线程随机休眠一段时间,让另外一个线程运行,请将程序补充完整。

    注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。

    class PutData extends Thread

    {

    DataPool s;

    int c;

    String name;

    public PutData(DataPool s,String name)

    {

    this.s=s;

    this.name=name;

    }

    public void run()

    {

    for(int i=0;i<10000000;i++)

    {

    c=(int)(Math.random()*10);

    s.setData(c);

    System.out.println(name+":push"+c);

    try

    {

    ______((int) (Math.random()*1000));//休眠

    }

    catch(InterruptedException e)

    {}

    }

    }

    }

    class DataPool

    {

    private int data=0;

    public ______void setData(int d)

    {

    data=d;

    }

    }

    public class simple

    {

    public static void main(String[] args)

    {

    DataPool s=new DataPool();

    PutData a=new PutData(s,"Thread a");

    PutData b=new PutData(s,"Thread b");

    a.start();

    b.start();

    }

    }


    正确答案:sleep synchronized
    sleep synchronized 解析:本题考查知识点:线程的要领和使用、线程的同步和共享。解题思路:线程类Thread提供了一系列控制线程的方法,让线程休眠,就是其中的一种。本题中定义了PutData的两个实例a和b。PutData的run方法中,利用一个循环向DataPool中存放10000000个随机数据,每存放一个数据以后休眠一段时间,让其他线程运行。第1个空,让线程休眠使用的方法是 sleep()。该方法使线程暂停运行一段时间,在这段时间内线程不会抢占CPU资源,为其他线程的运行提供了机会。本题的第2个空是考查线程的同步和互斥的问题,当线程A和线程B都调用setData方法改变data数值时,在同一段时间内,只能允许一个线程进行操作,这就需要两个线程之间互斥访问setData()方法。synchronized允许线程独占地存取对象,当 synchronized声明的程序块已经被一个线程占用,其他线程就必须等待该线程放弃这个程序块的控制权以后才能对其进行访问。因此,第2个空需要填写synchronized。

  • 第4题:

    下面程序段的输出结果是______。 public class Test{ public static void main(String args[ ]){ int x,y; x=(int)Math.sqrt(5/2)+(int)Math.random( )*5/2; y=(int)Math.sqrt(3/2)+(int)Math.random( )*3/2; if(x>y) System.out.println("x>y"); else if(x==y) System.out.println("x=y"); else System.out.println("x<y"); } }

    A.x>y

    B.x=y

    C.x<y

    D.编译错误


    正确答案:A
    解析: 本程序中两个赋值语句右边的表达式分别求两个标准函数的值,再进行整除,判断 x和y的大小。Math.sqrt( )方法计算一个数的平方根,Math.random( )方法输出[1,0]之间的随机数,本题中经过两条赋值语句,x=1,y=0。进入分支结构后,满足if条件执行相应的System.out.println("x>y")操作。所以输出结果应该是选项A。

  • 第5题:

    要产生[20,999]之间的随机整数使用哪个表达式?

    A.(int)(20+Math.random()*979)

    B. 20+(int)(Math.random()*980)

    C. (int)Math.random()*999

    D. 20+(int)Math.random()*980


    正确答案:B

  • 第6题:

    动作语句:math.random()*10表示产生0--10之间的随机数。

    A

    B



  • 第7题:

    在Math类中random方法可以产生随机数。如果从1到100之间产生一个随机整数(大于等于1,小于等于100),并赋值给n,那么下面语句正确的是()。

    • A、n=Math.round(Math.random(100))+1;
    • B、n=Math.round(Math.random()*100)+1;
    • C、n=Math.floor(Math.random(100))+1;
    • D、n=Math.floor(Math.random()*100)+1;

    正确答案:D

  • 第8题:

    棘轮机构除常用来实现间歇运动的功能外,还常用来实现什么功能?


    正确答案:制动器、单向离合器、超越离合器等。

  • 第9题:

    class Sock {  String size;  String color;  public boolean equals(Object o) {  Sock s = (Sock) o;  return color.equals(s.color);   }  // insert code here  }  哪两个满足 hashCode 的约定?()

    • A、public int hashCode() { return 343; }
    • B、public int hashCode() { return size.hashCode (); }
    • C、public int hashCode() { return color.hashCode (); }
    • D、public int hashCode() { return (int) (Math.random() * 1000);

    正确答案:B,C

  • 第10题:

    问答题
    图中A是什么,起到什么作用?

    正确答案:
    解析:

  • 第11题:

    单选题
    要产生[20,999]之间的随机整数可以使用以下哪个表达式?()
    A

    (int)(20+Math.random()*97)

    B

     20+(int)(Math.random()*980)

    C

    (int)Math.random()*999

    D

     20+(int)Math.random()*980


    正确答案: D
    解析: 暂无解析

  • 第12题:

    (难度:中等)Math.random()返回 0(不包括) 至 1(不包括) 之间的随机数

    答案:(no)

  • 第13题:

    执行int x=(int)(Math.random()*100);语句时,不会出现下列哪个数值?

    A.1

    B.35

    C.48

    D.100


    正确答案:D
    解析:Math.random()方法产生0~1之间的数,所以再乘以100的范围时0~100,但不包括边界0和100。

  • 第14题:

    下列声明语句中没有起到定义作用的是( )。

    A.int count;

    B.const double pi=3.14159;

    C.extern long index;

    D.int max(int a,int b){return a>b? a:b;}


    正确答案:C
    解析:在C++程序中,一个名称在使用之前必须先被声明。声明的目的是告诉编译器某个名称所指代的实体类型。选项A是对变量的声明和定义,选项B是常量的声明、定义和初始化,选项D是函数的声明和定义,选项C是对外部变量index进行了声明,而没有进行定义。

  • 第15题:

    下面程序段的输出结果是( )。 publicclassTest{ publicstaticvoidmain(Stringargs[]){ intx,y; x=(int)Math.sqrt(5)/2+(int)Math.random()*5/2; Y=(int)Math.sqrt(3)/2+(int)Math.random()*3/2; if(x>y) System.out.println9"x>y"); elseif(x==y) System.out.println("x=y"); else System.out.println("x<y"); } }

    A.x>y

    B.x=y

    C.x<y

    D.编译错误


    正确答案:A
    解析: 本题考查Java语言中if-else分支结构和几个标准函数语句的用法。本题中赋值号右边的两个表达式分别求两个标准函数的值,再进行整除,判断出x与y的大小。Math.sqrt计算一个数的平方根,Math.random()输出[0,1]之间的随机数,本题中经过两条赋值语句后,x=1,y=0。进入分支结构后,满足if条件执行相应System.out.pfintM("x>y");操作。

  • 第16题:

    阅读以下说明和 Java 代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 设计 RGB 方式表示颜色的调色板,进行绘图。其类图如图 6-1 所示。该程序的 Java代码附后。图6-1 类图

    【Java 代码】 //颜色类 class MyColor { private int red ,green, blue; public MyColor( ) { red = o; green = 0; blue = 0; } public MyColor(int red ,int green ,int blue) { this.red = red; this.green = green; this.blue = blue; } //其他方法略 public String toString( ) { return "Red: " + red + "\tGreen: " + green + "\tBlue " + blue; } } //调色板类 class Palette { public int number; / /颜色数 private (1)palette; //颜色表 public Palette( ) { number = 256; palette = new MyColor[number); } public Palette(MyColor[] palette ,int number) { (2)= number; (3)= palette; } //其他方法略 public String toString( ) { String str = ""; for (int i = 0; i < number; i++) { str +=i+ " : " + palette[i] + "\n"; } return str; } //绘图类 class Drawing { public (4) int COLORNUMBER = 16; public static void main(String[] args) { Palette palette; int red ,green ,blue; MyColor[] color = new MyColor[COLORNUMBER]; for (int i = 0; i < COLORNUMBER; i++) { red = (int) (Math.random( ) * 256); green = (int) (Math.random( ) * 256); blue = (int) (Math.random( ) * 256); color [i] = (5) (red ,green ,blue); } palette = new Palette(color ,COLORNUMBER); System.out.println(palette); } }


    正确答案:(1) MyColor[]
    (2) this.number
    (3) this.palette
    (4) static final
    (5) new MyColor

  • 第17题:

    第③段划线语句除举例子之外,还使用了哪两种说明方法?它们分别起到什么作用?(4分)


    正确答案:
    使用了作比较、列数字两种说明方法。
    通过作比较突出了臭椿对二氧化硫的灵敏反应。通过列数字准确地说明了臭椿对二氧化硫具有很强的吸收能力。

  • 第18题:

    Math.random()方法可返回介于()和()之间的一个伪随机数。


    正确答案:0;0

  • 第19题:

    Java语言中的方法Math.Random( )返回0到()之间的一个随机小数。


    正确答案:1

  • 第20题:

    以下()表达式产生一个0~7之间(含0,7)的随机整数。

    • A、Math.floor(Math.random()*6)
    • B、Math.floor(Math.random()*7)
    • C、Math.floor(Math.random()*8)
    • D、Math.ceil(Math.random()*8)

    正确答案:B

  • 第21题:

    Math.random()方法用来实现什么功能?下面的语句起到什么作用? (int)(Math.random()*6)+


    正确答案: 1Math.random()用于产生随机数(0到1之间,包含0,但不包含1)。
    上面的表达式表示1到6之间的一个随机整数(包含1及6)。

  • 第22题:

    填空题
    Java语言中的方法Math.Random( )返回0到()之间的一个随机小数。

    正确答案: 1
    解析: 暂无解析

  • 第23题:

    判断题
    动作语句:math.random()*10表示产生0--10之间的随机数。
    A

    B


    正确答案:
    解析: 暂无解析