美文网首页
双色球选号程序(面向对象)

双色球选号程序(面向对象)

作者: _Raye | 来源:发表于2017-04-04 10:59 被阅读0次

Ball类:

class Ball{
    private String color;
    private int num;

    public Ball(int num, String color){
        this.num = num;
        this.color = color;
    }

    public Color getColor(){
        return color;
    }

    public Num getNum(){
        return num;
    }
}

public static void main(String[] args){
    for(int i = 0; i < 6; i++){
        int num = (int)(Math.random * 33 + 1);
        Ball redBall = new Ball(num,red);
        int number = redBall.getNum();
        System.out.print(number + " ");
    }
    int num1 = (int)(Math.random * 16 + 1);
    Ball blueBall = new Ball(num1,blue);
    int number1 = blueBall.getNum();
    System.out.print(number1);

}

选号:

import java.awt.Color;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

class Ball implements Comparable<Ball> {
    private int number;
    private Color color;

    public Ball(int number, Color color) {
        this.number = number;
        this.color = color;
    }

    public int getNumber() {
        return number;
    }

    public Color getColor() {
        return color;
    }

    @Override
    public int compareTo(Ball other) {
        return this.number - other.number;
    }
}

class SelectNumberMachine {
    private static Random r = new Random();
    private List<Ball> redBalls = new ArrayList<>();
    private List<Ball> blueBalls = new ArrayList<>();

    public void reset() {
        redBalls.clear();
        for (int i = 1; i <= 33; ++i) {
            redBalls.add(new Ball(i, Color.RED));
        }
        blueBalls.clear();
        for (int i = 1; i <= 16; ++i) {
            blueBalls.add(new Ball(i, Color.BLUE));
        }
    }

    public String generate() {
        Ball[] currentRedBalls = selectRedBalls();
        Arrays.sort(currentRedBalls);
        Ball currentBlueBall = selectBlueBall();
        StringBuilder sb = new StringBuilder();
        for (Ball tempBall : currentRedBalls) {
            sb.append(String.format("%02d ", tempBall.getNumber()));
        }
        sb.append("| ");
        sb.append(String.format("%02d", currentBlueBall.getNumber()));
        return sb.toString();
    }

    private Ball[] selectRedBalls() {
        Ball[] currentRedBalls = new Ball[6];
        for (int i = 0; i < currentRedBalls.length; ++i) {
            int randomIndex = r.nextInt(redBalls.size());
            currentRedBalls[i] = redBalls.remove(randomIndex);
        }
        return currentRedBalls;
    }

    private Ball selectBlueBall() {
        int randomIndex = r.nextInt(blueBalls.size());
        return blueBalls.remove(randomIndex);
    }

}

class Test {

    public static void main(String[] args) {
        SelectNumberMachine machine = new SelectNumberMachine();
        for (int i = 0; i < 10; ++i) {
            machine.reset();
            System.out.println(machine.generate());
        }
    }
}

相关文章

  • 双色球选号程序(面向对象)

    Ball类: 选号:

  • 双色球选号程序(非面向对象)

  • 2018-07-13

    2018081期双色球选号参考资料 2018年081期双色球 貂蝉预测推荐 24红:01,02,04,05,06,...

  • 《软件工程》笔记7

    面向对象的实现 面向对象的程序设计最好还是选用面向对象的编程语言。 良好的程序设计风格对于面向对象实现来说格外重要...

  • 核心技术

    1.面向对象编程: 程序 = 对象 + 消息 //面向过程:程序 = 算法 + 数据 2.对象 对象的特征分为静态...

  • 类和对象

    1、面向对象程序设计的基本概念 过程是编程语言:程序=算法+数据面向对象编程语言:程序=对象+消息 对象 对象的特...

  • 《Java核心技术卷 I》之 Java对象与类

    Java对象与类 面向对象程序设计 面向对象程序设计,简称OOP,Java语言就是完全面向对象的。 类 类(cla...

  • 面向对象

    title:面向对象tags: 基础,Java 面向对象 面向对象程序设计简介 面向对象的基本思想是使用类、对象、...

  • oc编程思想

    面向对象概念 面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一...

  • 【从零开始学Java】学习笔记day012

    一 面向对象思想 1.1面向对象思想概述 概述 Java语言是一种面向对象的程序设计语言,而面向对象思想是一种程序...

网友评论

      本文标题:双色球选号程序(面向对象)

      本文链接:https://www.haomeiwen.com/subject/wepaottx.html