/*
应用for循环
用来计算抽奖的概率
*/
import java.util.*;
public class LotteryOdds {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("你要抽取几个数字:");
int x = in.nextInt();
System.out.println("从多少个数字中抽取:");
int n = in.nextInt();
int lotteryOdds = 1;// 初始化中奖概率
for(int i = 1; i<=x; i++){
lotteryOdds = lotteryOdds * (n - i + 1) / i;
}
System.out.println("你的中奖概率为:1/" + lotteryOdds);
}
}
Java中for循环的应用
最新推荐文章于 2024-07-14 13:21:09 发布