java编写随机返回1或者-1的条件表达式
解决方案 »
- static成员变量初始化,大虾帮忙解释下。谢谢!!!
- java多线程同步问题
- 麻烦问下在Web Service里面能返回Map对象吗?有现成的例子吗?
- 遇到SimpleDateFormat格式化日期的奇怪问题。
- 寻求编写java的工具,包括初学、熟悉、等阶段各应用什么工具?
- classpath的问题
- 请问为什么有些class编译出来后面都带有*$*.class的文件名呢?
- 关于jbuilder6使用的两个小问题,绝对给分
- 好奇怪的初始化问题。
- JNICALL是不是JAVA里面的东西?那JNICALL是什么类型?
- java 关于volatile读操作与volatile修饰引用类型变量的问题?
- 如何用java代码向word文档里固定位置导入图片
Random r = new Random();
int num = r.nextint(2)-1;
int a=r.nextInt()>0?1:-1;
System.out.println(r.nextBoolean()?1:-1);
取当前时间到1970-1-1 00:00:00的毫秒数,对2取余,搭配条件表达式就可以了
Calendar c=Calendar.getInstance();
int a=c.getTimeInMillis()%2==0?1:-1;
int num = 1- 2*r.nextint(2);
if (num==0) {
num=-1;
}
System.out.println(num);随机数大致平分
new Random().nextBoolean() ? 1 : -1
int result = 0;
double d = new Random().nextDouble();
result = (d>0.5)?1:-1;
return result;
}
int i = 0;
Random r = new Random();
boolean b = r.nextBoolean(); if (b) {
i = 1;
} else {
i = -1;
}
System.out.println(i);
}
int i;
boolean b = new Random().nextBoolean();
if (b) {
i = 1;
} else {
i = -1;
}
return i; }
public class Test22 {
public static void main(String[] args) {
double num = (Math.random() >= 0.5) ? 1 : -1;
System.out.print(num);
}
}
出现概率2:1诶对哦!这样可以吧
public class Test22 {
public static void main(String[] args) {
double num = (Math.random() >= 0.5) ? 1 : -1;
System.out.print(num);
}
}
Random random = new java.util.Random();
int c = random.nextInt(100)%2>0?-1:1;
System.out.println(c);
Random a=new Random();
int r=a.nextInt(1);//产生0到1的随机数
r=1?1:-1;//判断r是1吗如果是则返回1,不是则返回-1;