3.16 (Random point) Write a program that displays a random coordinate in a rectangle.
The rectangle is centered at (0, 0) with width 100 and height 200.
3.16(随机点)编写一个程序,显示矩形中的随即坐标,矩形剧中于(0.0),宽100,高200。
代码如下
public class Unite3Test16
{
public static void main(String[] args)
{
int width,height,m,n;
width=(int)(Math.random()*50);//正半轴50负半轴50
height=(int)(Math.random()*100);//正半轴100负半轴100
m=(int)(Math.random()*2);
n=(int)(Math.random()*2);
if(m==0)
{
width=width*-1;
}
if(n==0)
{
height=height*-1;
}
System.out.println("产生的随机坐标是:");
System.out.println("("+width+"."+height+")");
}
}
结果如下: