LotteryDrawing
2024-10-13 12:56:34
import java.util.*; public class MyTest{
public static void main(String[] args){
Scanner in = new Scanner(System.in); System.out.print("How many numbers do you need to draw? ");
int k = in.nextInt(); System.out.print("What is the highest number you can draw? ");
int n = in.nextInt(); //fill an array with numbers 1 2 3... n
int[] numbers = new int[n];
for(int i = 0; i < numbers.length; i ++)
numbers[i] = i + 1; //draw k numbers and put them into a second array
int[] result = new int[k];
for(int i = 0; i < result.length; i ++){
// make a random index between 0 and n - 1
int r = (int)(Math.random() * n); //pick the element at the random location
result[i] = numbers[r]; //move the last element into the random location\
//ps: I guess the author must consider the Poker when he write down these codes ..........
numbers[r] = numbers[n - 1];
n --;
} //print the sorted array
Arrays.sort(result);
System.out.print("Bet the following combination. It's make you rich!");
for(int r: result)
System.out.println(r);
}
}
最新文章
- qq空间返回顶部代码
- MySQL 5.7.x 配置教程
- 使用 Spring 3 来创建 RESTful Web Services(转)
- 对html进行SEO的一点点总结
- 关于cocos2d-x精灵加亮及变灰效果
- eclipse.ini内存设置
- [转]iOS UIAppearance使用详解
- 非对称加密算法RSA使用注意事项
- mac下利用Breakpad的dump文件进行调试
- Java_基础篇(数组排序)
- Maven Gradle
- volatile(一)
- C语言中简单的for循环和浮点型变量
- jQuery实现锚点跳转(就一行代码)
- [MySQL]修改root密码的4种方法(以windows为例)
- 设置dedecms标签 [field:global.autoindex/] 初始值{class递增}
- 检测是否为n的因子 Exercise07_06
- LINQ不包含列表
- hdu1233 继续畅通工程 (最小生成树——并查集)
- 转 Linux内存管理原理