Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 刽子手:如何用找到的字母来比较和替换破折号_Java - Fatal编程技术网

Java 刽子手:如何用找到的字母来比较和替换破折号

Java 刽子手:如何用找到的字母来比较和替换破折号,java,Java,我正在学习Java,我知道它在stackoverflow上有几种解决方案,但我被卡住了。我正试图创造一个基本的刽子手 我想知道如何用找到的字母替换破折号? 下面是一个演示: 要搜索的单词是:no 我输入字母n 我输入字母o 同上 这是我的密码: Scanner input = new Scanner(System.in); char letter = 0; // declares and initialises letter String[] words = {"yes", "no"}; //

我正在学习Java,我知道它在stackoverflow上有几种解决方案,但我被卡住了。我正试图创造一个基本的刽子手

我想知道如何用找到的字母替换破折号?

下面是一个演示:

要搜索的单词是:
no

  • 我输入字母
    n
  • 我输入字母
    o
  • 同上 这是我的密码:

    Scanner input = new Scanner(System.in);
    
    char letter = 0; // declares and initialises letter
    String[] words = {"yes", "no"}; // declares and initialises an array of words to guess
    String word = words[(int) (Math.random() * words.length)]; // chooses random word
    boolean[] found = new boolean[word .length()];
    int attempts = 5;
    
    while(attempts > 0){
      System.out.println("You have " + attempts + " attempts.");
      for(int i=0; i<word.length(); i++) {
        if ( found[i] ) {
          System.out.print(word.charAt(i));
        }
        else {
          System.out.print('-');
        }
      }
    
      System.out.println("");
      System.out.print("Enter your letter : ");
      letter = input.next().charAt(0);
    
      attempts--;
      }
    
    扫描仪输入=新扫描仪(System.in);
    字符字母=0;//声明并在信上签名
    字符串[]单词={“是”、“否”};//声明并初始化要猜测的单词数组
    字符串单词=单词[(int)(Math.random()*words.length)];//选择随机词
    boolean[]found=新的boolean[word.length()];
    int=5;
    而(尝试次数>0){
    System.out.println(“您有“+尝试次数+”尝试次数”);
    对于(int i=0;i

    可以使用
    String
    类中的方法检查字符是否在单词中

    应该是这样的:

    while (attemps > 0) {
        //...
        System.out.println("");
        System.out.print("Enter your letter : ");
        letter = input.next().charAt(0);
    
        int characterPosition = word.indexOf(letter);//use a loop because the character could appear in more than one position in the word
        while (characterPosition != -1) {//if indexOf(char) returns -1 it means the char was not found
            found[characterPosition] = true;
            characterPosition = word.indexOf(letter, characterPosition);//this time search the character starting from the last position to find the next one
        }
    
        attempts--;
    }
    

    使用调试器或print语句检查找到的
    中的值,您将看到问题注意
    System.out.println(“”
    )可以被
    System.out.println()替换。
    
    You have 3 attempts.
    --
    Enter your letter:
    
    Scanner input = new Scanner(System.in);
    
    char letter = 0; // declares and initialises letter
    String[] words = {"yes", "no"}; // declares and initialises an array of words to guess
    String word = words[(int) (Math.random() * words.length)]; // chooses random word
    boolean[] found = new boolean[word .length()];
    int attempts = 5;
    
    while(attempts > 0){
      System.out.println("You have " + attempts + " attempts.");
      for(int i=0; i<word.length(); i++) {
        if ( found[i] ) {
          System.out.print(word.charAt(i));
        }
        else {
          System.out.print('-');
        }
      }
    
      System.out.println("");
      System.out.print("Enter your letter : ");
      letter = input.next().charAt(0);
    
      attempts--;
      }
    
    while (attemps > 0) {
        //...
        System.out.println("");
        System.out.print("Enter your letter : ");
        letter = input.next().charAt(0);
    
        int characterPosition = word.indexOf(letter);//use a loop because the character could appear in more than one position in the word
        while (characterPosition != -1) {//if indexOf(char) returns -1 it means the char was not found
            found[characterPosition] = true;
            characterPosition = word.indexOf(letter, characterPosition);//this time search the character starting from the last position to find the next one
        }
    
        attempts--;
    }