questions.java
来自「java版的数据结构的完全代码 免费提供了 学习数据结构的请下载」· Java 代码 · 共 70 行
JAVA
70 行
// Introduced in Chapter 10import java.util.Scanner;/** The game of Questions. */public class Questions { /** For reading from the console. */ public static final Scanner INPUT = new Scanner(System.in); /** Root of the tree of knowledge. */ private BinaryNode<String> root; /** * Initially, the program guesses that the player is thinking of * a giraffe. */ public Questions() { root = new BinaryNode<String>("a giraffe"); } /** * Node is a leaf corresponding to an incorrect guess. Gather * information from the user and add two children to node. */ protected void learn(BinaryNode<String> node) { System.out.print("What is it? "); String correct = INPUT.nextLine(); System.out.println("I need a question to distinguish that from " + node.getItem() + "."); System.out.println("The answer for " + correct + " should be yes."); System.out.print("Enter the question: "); String question = INPUT.nextLine(); node.setLeft(new BinaryNode<String>(correct)); node.setRight(new BinaryNode<String>(node.getItem())); node.setItem(question); } /** Play until the program wins or gives up. */ public void play() { BinaryNode<String> node = root; while (!(node.isLeaf())) { System.out.print(node.getItem() + " "); if (INPUT.nextLine().equals("yes")) { node = node.getLeft(); } else { node = node.getRight(); } } System.out.print("It is ... " + node.getItem() + "? "); if (INPUT.nextLine().equals("yes")) { System.out.println("I win!"); } else { System.out.println("I give up."); learn(node); } } /** Create and repeatedly play the game. */ public static void main(String[] args) { Questions game = new Questions(); System.out.println("Welcome to Questions."); do { System.out.println(); game.play(); System.out.print("Play again (yes or no)? "); } while (INPUT.nextLine().equals("yes")); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?