命运之轮:建筑弦

时间:2017-01-17 14:43:46

标签: java

我正在编写一个分配代码,我必须在其中制作Wheel of Fortune,我在构建将在每次猜测后显示给用户的字符串时遇到问题。我有轮子旋转工作,但我似乎无法想象如何构建字符串。如果你有任何问题,我也可以回答任何问题。任何帮助都感激不尽。到目前为止,这是我的代码:

class WheelOfFortune3 {
    public static void main(String[] args) {
        int result, location, newLocation, numGuess = 0;
        String puzzle, guess;
        String sub[] = new String[26];
        for (int i = 0; i < sub.length; i++) {
            sub[i] = "_";
        }

        result = wheelResult();
        System.out.println("You spun $" + result);
        puzzle = getPuzzle();
        System.out.println(puzzle);



        do {
            guess = In.getString();
            location = checkGuess(puzzle, guess);
            if (location == -1) {
                System.out.println("Incorrect guess");
            }
            if (location >= 0 && location <= 25) {
                System.out.println("Correct guess");
                newLocation = location + 1;
                sub[numGuess] = puzzle.substring(location, newLocation);
                for (int i = 0; i <= 10; i++) {
                    System.out.print(sub[i] + " ");
                }
                System.out.println("");
                numGuess = numGuess + 1;
                System.out.println(numGuess);
                System.out.println(sub.length);



            }
        }
        while (numGuess < sub.length);
    }


    public static int checkGuess(String puzzle, String guess) {

        String word = puzzle;
        int location;
        location = word.indexOf(guess);
        return location;

    }

    public static int wheelResult() {
        int result;
        int[] wheelSpin = new int[1];
        wheelSpin[0] = (int)(24 * Math.random());


        if (wheelSpin[0] == 1 || wheelSpin[0] == 18 || wheelSpin[0] == 22) {
            result = 200;
            return result;
        } else if (wheelSpin[0] == 2 || wheelSpin[0] == 5) {
            result = 900;
            return result;
        } else if (wheelSpin[0] == 0 || wheelSpin[0] == 3 || wheelSpin[0] == 15) {
            result = 250;
            return result;
        } else if (wheelSpin[0] == 4 || wheelSpin[0] == 6 || wheelSpin[0] == 12 || wheelSpin[0] == 16) {
            result = 300;
            return result;
        } else if (wheelSpin[0] == 7) {
            result = 1500;
            return result;
        } else if (wheelSpin[0] == 9 || wheelSpin[0] == 11) {
            result = 700;
            return result;
        } else if (wheelSpin[0] == 10 || wheelSpin[0] == 14 || wheelSpin[0] == 21) {
            result = 500;
            return result;
        } else if (wheelSpin[0] == 13) {
            result = 5000;
            return result;
        } else if (wheelSpin[0] == 23 || wheelSpin[0] == 19) {
            result = 600;
            return result;
        } else if (wheelSpin[0] == 8 || wheelSpin[0] == 20) {
            result = 100;
            return result;
        } else if (wheelSpin[0] == 17) {
            result = 17;
            return result;
        }

        return -1;
    }
    public static String getPuzzle() {
        String puzzle;
        //a long ride
        return "A long ride";
        //01234567890
    }
}

1 个答案:

答案 0 :(得分:1)

可以使用List Character.contains()方法完成此操作:

List<Character> guessedCharacters = new ArrayList<>();

每次玩家猜到一封信,都要加上那封信 像这样的guessedCharacters列表:guessedCharacters.add(char);

然后你可以做这样的事情来生成String输出到播放器:

StringBuilder toShowSB = new StringBuilder();
for(int i=0,n=puzzle.length;i<n;i++){
    if(guessedCharacters.contains(puzzle.charAt(i))){
        toShowSB.append(puzzel.charAt(i));
    }else{
        toShowSB.append("_");
    }
}
String toShow = toShowSB.toString();

这将创建一个String,其中包含所有猜到的字母,并且下划线表示尚未正确猜到的字符。