日期:2014-05-20 浏览次数:21110 次
import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class GuessGame { List<String> words = new ArrayList<String>(); String word = ""; String matchs = " "; int missTime = 0; GuessGame(String... args) { words.addAll(Arrays.asList(args)); } public void randomWords() { int temp = (int) (Math.random() * words.size()); word = words.get(temp); } public void guess() { System.out.print("Enter a letter in word " + word.replaceAll("[^" + matchs + "]", "*") + " >>>"); BufferedInputStream in =new BufferedInputStream(System.in); char c = ' '; try { c = (char)in.read(); } catch (IOException e) { e.printStackTrace(); } if (matchs.indexOf(c) != -1) { System.out.println(c + " is already in the word"); guess(); return; } else{ if(word.indexOf(c) != -1){ matchs+=String.valueOf(c); String temp = word.replaceAll("[^" + matchs + "]", "*"); if(temp.indexOf('*') == -1){ System.out.println("the word is "+word+" .you missed "+ missTime +" time"); } else{ guess(); } } else{ System.out.println(c+" is not in the word"); missTime++; guess(); } } } public static void main(String args[]){ GuessGame gg=new GuessGame("welcome","helloword","niceshoot"); boolean flag = true; while(flag){ gg.randomWords(); gg.guess(); System.out.println("Do you want to guess for another word ? Enter y or n>>>"); BufferedInputStream in =new BufferedInputStream(System.in); try { if((char)in.read() == 'n'){ flag = false; } } catch (IOException e) { e.printStackTrace(); } gg.missTime =0; gg.matchs = " "; } } }
------解决方案--------------------
还是有点意思,所以写了个,你看看吧
package com.test;
import java.util.Random;
import java.util.Scanner;
public class Test {
    
    static char[] word;
    static char[] printWord;
    static String singleWord = "";
    static int count;
    
    public static void main(String[] args) {
        boolean over = false;
        word = randomWord().toCharArray();
        printWord = new char[word.length];
        for(int i = 0; i < word.length; i++){
            printWord[i] = '*';
        }
        Scanner scan = new Scanner(System.in);
        while(true){
            if(!over){
                System.out.println("(guess) Enter a letter in word " + new String(printWord) + " > ");
                String in = scan.next();
                if(in.length() == 1){
                    char c = in.charAt(0);
                    boolean haveChar = false;
                    if(singleWord.indexOf(c) == -1){
                        for(int i = 0; i < word.length; i++){
                                if(c == word[i]){
                                    printWord[i] = c;
                                    singleWord += String.valueOf(c);
                                    haveChar = true;
                                }
                        }
                    }else{
                        haveChar = true;
                        System.out.println(String.valueOf(c) + " is already in the word");
                    }
                    if(!haveChar){
                        count++;
                        System.out.println(String.valueOf(c) + " is not in the word");
                    }
                    if(String.valueOf(printWord).indexOf("*") == -1){
                        System.out.println("the word is " + String.valueOf(word));
                        System.out.println("the word is program.you missed " + count + " time");
                        System.out.println("Do you want to guess for another word? Enter y or n >");
                        over = true;
                    }
                }
            }else{
                String in = scan.next();
                if("y".equalsIgnoreCase(in)){
                    over = false;
                    word = randomWord().toCharArray();
                    printWord = new char[word.length];
                    singleWord = "";
                    count = 0;
                    for(int i = 0; i < word.length; i++){
                        printWord[i] = '*';
                    }
                }else if("n".equalsIgnoreCase(in)){
                    break;
                }else{
                    System.out.println("Do you want to guess for another word? Enter y or n >");
                    continue;
                }
            }
        }
        
    }
    
    public static String randomWord(){
        String[] words = new String[]{"hello", "word", "jack", "hehehehe", "honghe", "beibi"};
        Random random = new Random();
        return words[random.nextInt(6)];
    }
}