java随机负数_java 为什么y 不能随机出来一个负数。

本文档展示了如何使用Java编程语言通过生成随机数来判断并输出在笛卡尔坐标系中的象限位置。通过Math.random()函数生成双精度浮点数,计算并根据奇偶性决定x和y坐标的值,最后输出四象限结果及具体坐标值。
摘要由CSDN通过智能技术生成

publicclassJudge{publicstaticvoidmain(String[]args){doublea1;doublea2;doublea3;doublea4;intx=1;inty=1;intn;intn1;intc=0;intd=0;for(n=1;n<=10;n++){a1=Math.random();a2=Math...

public class Judge {

public static void main(String[] args) {

double a1;

double a2;

double a3;

double a4;

int x = 1;

int y = 1;

int n;

int n1;

int c = 0;

int d = 0;

for (n = 1; n <= 10; n++) {

a1 = Math.random();

a2 = Math.random();

c = (int) (a1 * 10);

if (c % 2 == 0) {

x = (int) (a2 * 10);

} else if (c % 2 == 1) {

x = (int) (a2 * 10);

x = x - x * 2;

}

}

for (n1 = 1; n1 <= 100; n1++) {

a3 = Math.random();

a4 = Math.random();

d = (int) (a3 * 10);

if (d % 2 == 0) {

y = (int) (a4 * 10);

} else if (d % 2 == 1) {

y = (int) (a4 * 10);

y = y - y * 2;

}

}

if (x > 0 && y > 0) {

System.out.println("第一象限");

}

if (x < 0 && y > 0) {

System.out.println("第二象限");

}

if (x < 0 && y < 0) {

System.out.println("第三象限");

}

if (x > 0 && y < 0) {

System.out.println("第四象限");

}

System.out.println("(" + x + "," + y + ")");

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值