Hangman.java用猜出的正确字母替换空白

时间:2013-11-20 09:12:55

标签: java

我希望我的刽子手游戏能够用单词中的正确字母替换空白“_”。

这就是我的程序看起来像atm:

package hängagubbe;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;

public class hangdamang {

    public static void main(String[] args) throws FileNotFoundException {

        // Engine
        Scanner input = new Scanner(System.in);
        Scanner ord = new Scanner(new File("c:/words.txt"));        
        int gissningar = 0;
        String blanks = "";
        String guessword;
        String rightLetters = "";
        String guess;
        // Arraylist, här sparas alla ord
        ArrayList<String> arswag = new ArrayList<String>();
        // while loop, tar ord från words.txt och lägger till arraylisten.
        while(ord.hasNext()) {
            arswag.add(ord.nextLine());
        }
        //Väljer ett random ord i arraylisten.
        Collections.shuffle(arswag);
        String pickWord = arswag.get(0);
        System.out.println(pickWord);
        int w=pickWord.length();
        //Delar det random valda ordet till endast bokstaver till arrayen
        String [] splitword = pickWord.split("");
        //
        for(int i = 1; i <= pickWord.length(); i++){
            blanks +="-";
            }

        //Riktiga spelet:
        System.out.println("Välkommen till mitt hänga gubbe spel.");
        System.out.println("Längden på ordet du ska gissa är: " + w);
        System.out.println(blanks);
        do{ 
            System.out.println("\n" + "Skriv en bokstav för att gissa ordet.");
            System.out.println("Antal gissningar du gjort:" + gissningar);
            gissningar++;
            guess = input.next();
           for(int y=1; y < splitword.length; y++) {
            if (guess.equals(splitword[y])) {
                 System.out.print(guess );

            }
             else
                 System.out.print(" _ ");
           }
      } while (true);

}
}

正如你所看到的那样,我到目前为止你已经猜到了你可以猜字母,当这个词例如测试时,你猜它,它会说:

_ _ s _

但是当你再次猜测并做T时,它会说:

t _ _ t

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

我玩你的代码玩得很开心,这就是我得到的。我在这里和那里写了一些笔记,希望它能给你一个开始:)

// NOTE: java naming convntion: capital class names
// complete documentation see here: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
public class Hangdamang {

    public static void main(String[] args) throws FileNotFoundException {

        // Engine
        // NOTE: it is c-style to declare all needed variables in top of the method
        // in java you can declare them close to where you need them. Provides better readability at no costs.
        Scanner input = new Scanner(System.in);
        Scanner ord = new Scanner(new File("words.txt")); // for example zyzzyva.net/wordlists.shtml        
        int gissningar = 0;
        // Arraylist, här sparas alla ord
        ArrayList<String> arswag = new ArrayList<String>();
        // while loop, tar ord från words.txt och lägger till arraylisten.
        while(ord.hasNext()) {
            arswag.add(ord.nextLine());
        }
        //Väljer ett random ord i arraylisten.
        Collections.shuffle(arswag);
        String pickWord = arswag.get(0).split(" ")[0].toLowerCase();
        System.out.println(pickWord);

        // hold a list of not yet guessed letters 
        List<Character> unknownLetters = new LinkedList<Character>(); // NOTE: you could as well use ArrayList
        for(Character c = 'a'; c<='z'; c++)
            unknownLetters.add(c);

        //Riktiga spelet:
        System.out.println("Välkommen till mitt hänga gubbe spel.");
        System.out.println("Längden på ordet du ska gissa är: " + pickWord.length());
        String guess="";
        String showWord="";
        while(!guess.equalsIgnoreCase(pickWord) && !showWord.equalsIgnoreCase(pickWord)) {

            System.out.println(showWord);

            System.out.println("\n" + "Skriv en bokstav för att gissa ordet.");
            System.out.println("Antal gissningar du gjort: " + gissningar);
            gissningar++;
            guess = input.next();

            if(guess.length()==1) {
                unknownLetters.remove(Character.valueOf(guess.charAt(0)));
                showWord = pickWord;
                for(Character c : unknownLetters)
                    showWord=showWord.replaceAll(c.toString(), "_");
            }

        }

        System.out.println("You won! Tries needed: "+gissningar);

    }
}

java命名约定的文档:http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html

答案 1 :(得分:0)

那是因为你没有打印下面if-else中的先前猜测。您可以打印当前猜测的当前猜测,也可以打印下划线(_),因此,前一个猜测不可见。

if (guess.equals(splitword[y])) {
    System.out.print(guess);
} else {
    System.out.print(" _ ");
}

如果已存在猜测值,则需要添加额外的检查以打印上一个猜测值。为此你需要一个额外的数组来保存先前的猜测或猜测填充的字符串。