最近忙着弄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);
}
}
}
|
阅读(1223) | 评论(0) | 转发(0) |