Java实现的抽取随机号码或者出场顺序
来源:互联网 发布:看图软件 知乎 编辑:程序博客网 时间:2024/05/16 17:51
本程序可以实现抽取随机号码或者抽取出场顺序
详情请看注释。
import java.util.Arrays;
import java.util.Scanner;/***
*
* @author 陈钊 本程序可以实现抽取幸运数字和抽取出场顺序
*/
public class Drawing {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Please input the number you need to draw:");
int k = sc.nextInt();
System.out.println("Please input the number you can draw:");
int n = sc.nextInt();
int temp_n = n;//store n represent :when n==k,this is a problem focus
//on sequence of drawing numbers.
if (k > n) {
System.out.println("K can not more than N!");
System.exit(-1);
}
// 初始化抽取数组
int[] numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = i + 1;
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
// produce a number between 0~n-1
int r = (int) (Math.random() * n);
result[i] = numbers[r];
// move the last element to the random location,
// in case drawing the same number
numbers[r] = numbers[n - 1];
n--;
}
//假设n与k相等就是抽取出场顺序
if (temp_n != k) {
Arrays.sort(result);
}
System.out.println("Result as following:"+Arrays.toString(result));
for (int a : result) {
System.out.println(a);
}
}
}
0 0
- Java实现的抽取随机号码或者出场顺序
- 图:出场顺序号码随机抽取及公开展示,并行随机抽取多个题目号码及公开展示-软件原型设计
- [出场顺序号码随机抽取并公开展示、并行随机抽取多个题目号码,并公开展示]软件原型设计
- 随机排列出场顺序
- MYSQL的随机抽取实现方法
- java随机抽取数组里的数据?
- 一个用JS编写的随机抽取号码的小程序
- java实现一个在窗口显示一个随机生成的号码
- SQL的随机抽取
- java随机抽取对象(图片的随机播放、抽取幸运观众 等)
- java list随机抽取元素
- java 随机数 随机打乱数组 随机抽取
- 随机抽取几条数据库记录的Java代码
- Java随机抽取方法、去重的方法
- java随机抽取指定范围内不重复的n个数
- 关于从list或者数组中随机抽取部分不重复元素的问题探究
- 随机抽取试题的方法
- 随机抽取的sql语句
- 构造方法
- 验证码插件kaptcha的jsp使用
- Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess 定理Lucas求大组合数
- C#高级编程五十三天----字典Dictionary<TKey,TValue>
- XHTML与HTML的区别
- Java实现的抽取随机号码或者出场顺序
- 细说css3 flex布局之flex容器属性
- 常见CSS布局疑难问题及其解决
- 常见邮件服务器
- nodejs学习笔记一
- 简单的字谜游戏--可扩展--2015年7月25日14:58:00V1.1版
- JDBC快速入门
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记38 Unwind Segue反向过渡
- codeforces 306 div.2 B. Preparing Olympiad