有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的基本刽子手游戏(主要涉及字符串操作)

我正在学习Java的入门课程,我们有一个关于刽子手游戏的项目。我已经完成了大部分代码,但是有一个bug我似乎无法解决

首先,程序会提示用户输入一个字母和一个空格,然后程序会在一系列连字符中显示一个单词,如果用户猜测正确,相应连字符中的字母将替换为所述字母

出于测试目的,单词已默认为narrow

因此,如果我猜字母r,对于空格,我猜索引2,程序会给我:

Guess a letter: r 

Guess a space:  2

Guess:    --r---

我遇到的问题是,当我猜测空间的索引3并尝试猜测下一个r时,程序只会给出与之前相同的输出

我们不允许使用数组或字符串生成器,因为我们还没有讨论它们

以下是我的变量:

    // default word for testing
    String blank = "narrow";

    // variables to manipulate the string
    String start = "";
    String end = "";
    String word = "";

    // variables for input and output
    // input is used for the user's letter guess
    String input; 
    // space is used for the user's index guess
    String space = "";
    // guess is used at the end to display the word to the user and set equal to word after
    // it has been manipulated
    String guess = "";

下面是操作字符串的代码

    for (int i = 0; i < blank.length(); i++) {
         if (i == blank.indexOf(input)) {
             start = guess.substring(0, i);
             end = guess.substring(i + 1);
             word = start.concat(input).concat(end);                    
         }
    }

我认为这和if statement有关,但我尝试过其他一些方法,但都不起作用。任何帮助都将不胜感激

多谢各位


共 (4) 个答案

  1. # 1 楼答案

    indexOf(String str)返回指定子字符串第一次出现时该字符串内的索引。更多信息here

    我建议最好的方法是,只有在用户正确的情况下才更改输出。因此,对于我的每一个猜测:

    if(blank.charAt(space) == input.charAt(0))
    {
         start = guess.substring(0, space);
         end = guess.substring(space + 1);
         word = start.concat(input).concat(end);
    }
    
  2. # 2 楼答案

    我会这样写:

    //set up variables
    Scanner keyboard = new Scanner(System.in);
    String word = "narrow";
    String display = "";
    for (int i = 0; i < word.length(); i++) {
      display = display + "-";
    }
    
    //loop until the word is guessed
    while (display.contains("-")) {
    
      //show the user flow, take input
      System.out.println("Guess: " + display);
      System.out.print("Guess a letter: ");
      String letter = keyboard.nextLine();
      System.out.print("Guess a space: ");
      String spaceStr = keyboard.nextLine();
      int space = Integer.parseInt(spaceStr);
    
      //check if the guess is right
      if (letter.equals(word.charAt(space) + "")) {
    
        //modify the string shown to the user
        String temp = display.substring(space + 1);
        display = display.substring(0, space);
        display = display + letter + temp;
      }
    }
    

    关键是要有一个向用户显示的变量和一个保存真实单词的变量。当他们做出正确猜测时,您可以修改显示给用户的字符串

  3. # 3 楼答案

    你的代码的问题是每次都是空白。indexOf(input)每次返回2(indexOf返回'r'的第一次出现,即2)

    可以更改条件以检查用户猜测的空格处的字符是否包含用户猜测的字母。 您可以按如下方式执行此操作:

    1. 保留要打印的图案。为此创建一个变量
    2. 每次用户正确猜测时更新模式

    注意:在下面的代码中,guess是我所说的变量,它最初被设置为“----”表示单词“窄”

        // check if the space has the letter you guessed
        if (blank.charAt(space) == input.charAt(0)) {
             // if it has just update the pattern string to also contain the new letter
             guess = guess.substring(0, space) + input + guess.substring (space + 1)
    

    您可以打印或返回(如果是方法)模式字符串

  4. # 4 楼答案

    我认为blank.indexOf(input)只返回该input字符的第一个出现索引。所以你需要使用thisindexOf(int ch, int fromIndex)。 在您的例子中,将输入字符最后一次出现的索引存储在某个int变量中,然后将其用作fromIndex

    int lastOccurrence = 0; 
    for (int i = 0; i < blank.length(); i++) {
         if (i == blank.indexOf(input, lastOccurrence)) {
             lastOccurrence = i; 
             start = guess.substring(0, i);
             end = guess.substring(i + 1);
             word = start.concat(input).concat(end);                    
         }
    }