测试你的OO水平

昨天去参加面试,先是和老美电话聊,聊的不错,然后老美给了道测试题,让40分钟做完,第一次做这样的TEST,有点找不到方向,我做的时候只想着给出个可运行的程序,没有想太多OO。呵呵,看看javaEye上的高手,谁能写个完美的例子出来。主要是测试OO能力!

 

Write a toy program to play a dice guessing game. To begin the game, 5 dices are tossed. The computer player you program will then guess what are those numbers without knowing it. After each guess, the computer player is told how many of its guesses are correct without being told which ones. If all dices are guessed correctly, game ends. Otherwise, the computer player guesses again. The objective of the computer player is to be able guess all dices right in the least number of rounds. Your program needs to be object-oriented. Use your own ingenuity and do not copy other's work.

 

Example:

 

Dices tossed: 1 2 3 5 5

 

Round 1:

Computer guesses: 2 2 3 5 6

Correct: 3

 

Round 2:

Computer guesses: 1 2 5 6 6

Correct: 2                                                                              

 

 

Round X:

Computer guesses: 1 2 3 5 5

Correct: 5. End game!

 

import java.io.IOException;
import java.util.Random;

/**
 * 
 * @author liur
 * 
 */
public class DiceGuess {

	private String randomDice = "";

	private String userInput = "";

	public DiceGuess() {
		int randomNum = new Random().nextInt(100000);
		randomDice = randomNum + "";
		System.out.println("Dices tossed :" + randomDice);

	}

	/**
	 * read the user's input
	 * 
	 * @return
	 */
	private static String readLine() {
		char nextChar;
		String result = "";
		boolean done = false;

		while (!done) {
			nextChar = readChar();
			if (nextChar == '\n')
				done = true;
			else if (nextChar == '\r') {
			} else
				result = result + nextChar;
		}
		return result;
	}

	private static char readChar() {
		int charAsInt = -1;
		try {
			charAsInt = System.in.read();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			System.out.println("Fatal error.Ending program.");
			System.exit(0);
		}
		return (char) charAsInt;
	}


	private static boolean validate(String userInput) {
		boolean isTure = false;
		if (userInput.length() != 5) {
			System.out.println("your input is not correct,pls give 5 numbers");
		} else {
			isTure = true;
		}
		return isTure;
	}

	private static int compare(String randomDice, String userInput) {
		char[] randomChar = randomDice.toCharArray();
		char[] inputChar = userInput.toCharArray();
		if(randomChar.length!=inputChar.length){
			return 0;
		}
		int count = 0;
		for (int i = 0; i < randomChar.length; i++) {
			if (randomChar[i] == inputChar[i]) {
				count++;
			}
		}
		return count;
	}

	public String getUserInput() {
		System.out.println("Pls give your guess");
		userInput = DiceGuess.readLine();
		if(!this.validate(userInput)){
			getUserInput();
		}
		return userInput;
	}

	public void excute() {
		int count = 0;
		do {
			userInput = getUserInput();
			count = DiceGuess.compare(randomDice, userInput);
			System.out.println("Correct : " + count);

		} while (count != 5);
		System.out.println("Congratulations!!!You are so smart! 2");

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DiceGuess diceGuess = new DiceGuess();
		diceGuess.excute();
	}
}

下面的代码是王成同学的杰作,非常感谢!他的博客 http://wangcheng.iteye.com/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值