简单的彩票中奖程序

package demo;

import java.io.FileOutputStream;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;

 

public class Ball {

    /**

     * 简单的彩票中奖程序

     */

    private int count = 0;// 计数器

    // 一调用此方法就开始摇球

    public void start() {

       Scanner sc = new Scanner(System.in);

       System.out.println("希望你中奖...");

       // 1.避免出现重复的号码,用了HashSet来保存信息,避免了重复问题

       Set set = new HashSet();

       while (set.size() < 7) {

           Random r = new Random();

           int num = r.nextInt(36) + 1;

           set.add(num);

       }

       // 2.产生的号码经由迭代,通过FileOutputStream存入a.txt文件中

       Iterator it = set.iterator();

       FileOutputStream fos = null;

       try {

           fos = new FileOutputStream("a.txt", true);// true追加信息

           fos.write("本期中奖号码: ".getBytes());

           while (it.hasNext()) {// 迭代器判断是否存在集合元素

              String strnum = ((Integer) it.next()).toString();

              System.out.println(" " + (count + 1) + "个摇出来的的号码是: " + strnum);

              sc.next();

              fos.write(strnum.getBytes());

              fos.write("/t".getBytes());

              count++;

           }

           fos.write("/r/n".getBytes());

       } catch (Exception e) {// 捕获异常

           e.printStackTrace();

       } finally {

           if (fos != null) {// 若输出流不为空,则关闭输出流。

              try {

                  fos.close();

              } catch (Exception e) {

                  e.printStackTrace();

              }

           }

       }

    }

 

    public static void main(String[] args) {

       Ball b = new Ball();// main中调用方法

       b.start();

    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值