
java中如何让A的输出的概率为50%B输出的概率为30%C输出的概率为20% 10
5个回答
展开全部
public class Test {
public static void main(String[] args) {
double d = Math.random();//生成一个0~1的随机数
if(d<=0.5){
System.out.println("A");//50%概率
}else if(d<=0.8){
System.out.println("B");//30%概率
}else{
System.out.println("C");//20%概率
}
}
}
展开全部
你好,可以这样写:
Random r = new Random();
int n = r.nextInt(10);
if (n < 2) {
System.out.println("C");
} else if (n >= 2 && n < 5) {
System.out.println("B");
} else {
System.out.println("A");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
publicclass test {
//橘子
publicstaticintjz = 5;
//香蕉
publicstaticintxj = 3;
//苹果
publicstaticintpg = 2;
publicstaticvoid main(String[] args) {
Random rnd = new Random();
int tempInt = rnd.nextInt(10);
System.out.println("tempInt====" +tempInt);
if (tempInt >= 0 && tempInt <= jz)
{
System.out.println("橘子");
}elseif (tempInt > jz && tempInt <= xj+jz) {
System.out.println("香蕉");
}elseif (tempInt > xj && tempInt <= pg+xj+jz) {
System.out.println("苹果");
}
}
}
//橘子
publicstaticintjz = 5;
//香蕉
publicstaticintxj = 3;
//苹果
publicstaticintpg = 2;
publicstaticvoid main(String[] args) {
Random rnd = new Random();
int tempInt = rnd.nextInt(10);
System.out.println("tempInt====" +tempInt);
if (tempInt >= 0 && tempInt <= jz)
{
System.out.println("橘子");
}elseif (tempInt > jz && tempInt <= xj+jz) {
System.out.println("香蕉");
}elseif (tempInt > xj && tempInt <= pg+xj+jz) {
System.out.println("苹果");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JAVA有个随机函数。设置输出概率还不知道
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询