欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

测试你的OO水平

程序员文章站 2022-03-13 10:54:05
...

昨天去参加面试,先是和老美电话聊,聊的不错,然后老美给了道测试题,让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/
相关标签: OO 面试