Chinaunix首页 | 论坛 | 博客
  • 博客访问: 696116
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: Java

2009-04-29 16:02:25


最近忙着弄java,没空写博客。觉得这个还可以,就放上来。


package cn.youhap.collection;

import java.util.Vector;

public class GuessingGame {
    private int target;
    private Vector guess = new Vector(100, 25);
    
    public GuessingGame(int g){
        super();
        this.setTarget(g);
    }
    public void setTarget(int target) {
        this.target = target;
    }
    public int getTarget() {
        return target;
    }
    
    public void startGuessing(){
        int temp = this.genRandom();
        Integer tempObj = null;
        while (this.getTarget() != temp){
            temp = this.genRandom();
            tempObj = new Integer(temp);
            this.guess.add(tempObj);
        }
    }
    
    public int genRandom(){
        return (int)(Math.random() * 100 + 1);
    }
    
    public void printGuessing(Vector v){
        for (int i = 0; i < v.size(); i++){
            System.out.println(v.get(i));
        }
        System.out.println("There are :" + this.guess.size() + " elements in " + this.guess.toString());
        System.out.println("The capacity is :" + this.guess.capacity());
    }
    
    public static void main(String [] cao){
        if (cao.length != 1){
            System.out.println("You must input a number. Bye...");
        }else{
            int inputNum = Integer.parseInt(cao[0]);
            GuessingGame gg = new GuessingGame(inputNum);
            gg.startGuessing();
            gg.printGuessing(gg.guess);
        }
    }
}

阅读(1147) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~