第三章第十六题(随机点)(random point)

    科技2022-07-12  113

    第三章第十六题(随机点)(random point)

    3.16(随机点)编写程序,显示矩形中一个随机点的坐标。矩形中心位于(0,0)、宽100、高200。 3.16(Random point) Write a program that displays a random coordinate in a rectangle. The rectangle is centred at (0, 0) with width 100 and height 200.参考代码: package chapter03; public class Code_16 { public static void main(String[] args) { int randomX,randomY,directionX,directionY; randomX = (int)(Math.random()*50); randomY = (int)(Math.random()*100); directionX = (int)(Math.random()*2); directionY = (int)(Math.random()*2); if(directionX == 0) randomX *= -1; if(directionY == 0) randomY *= -1; System.out.println("The random point is (" + randomX + "," + randomY + ")"); } } 结果显示: The random point is (-16,98) Process finished with exit code 0
    Processed: 0.010, SQL: 8