【问题标题】:Hangman in Java throws ArrayIndexOutOfBoundsJava 中的 Hangman 抛出 ArrayIndexOutOfBounds
【发布时间】:2014-03-30 20:21:06
【问题描述】:

我的程序在这段代码中抛出 StringIndexOutOfBoundsException:temp1 = temp.replace('-', temp.charAt(p)); 我正在尝试获取相同字母的索引(在比较输入的字母和单词之后)并删除“-”以表明用户猜对了.** **我已经尝试了几个小时无济于事。我认为问题在于我的循环。感谢您的回答:)如果我违反了什么,请原谅我。

run:
-----
Enter a letter:
a
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:

3 在 java.lang.String.charAt(String.java:658) 在 Hangman.main(Hangman.java:34) Java 结果:1

import java.util.Scanner;

public class Hangman {

    public static void main (String [] args){

    Scanner sc = new Scanner(System.in);

    String word = "Sample";
    String temp = null;
    String temp1 = null;
    String letter = null;
    int n;
    int m=0;
    int p = 0;

         for (n = 0; n<word.length(); n++){

                temp = word.replaceAll(word, "-"); //replaces the String word with "-" and prints
                System.out.print(temp); 

           }

         while (m<=5){ //the player can only guess incorrectly 5 times
                System.out.println("\nEnter a letter:");
                letter = sc.nextLine();

                letter.toLowerCase();

                if (word.contains(letter) == true){
                    p = word.indexOf(letter);
                    temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
                    System.out.print(temp1);
                }

                else {
                    System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
                    m++; //to count for incorrect guesses
                }    

                System.out.print(temp1);
        }


                System.out.println("Game Over.");
         }

    }

【问题讨论】:

  • temp1 = temp.replace('-', temp.charAt(p));在这一行中,使用 word.charAt(p) 代替 temp.charAt(p) 因为 temp 是 ----- 而不是在您编写 temp = word.replaceAll(word, "-"); 时采样这将帮助你.....
  • 啊,是的,我现在可以看到了。所以我尝试替换 temp1 = temp.replace('-', temp.charAt(p));到 temp1 = temp.replace('-',word.charAt(p));。但这就是发生的事情: ------ 输入一个字母: a aa 输入一个字母: 它只打印到索引。我似乎无法正确实现“char by char print”,我认为这可能会解决这个问题。
  • 打印两次因为你在写 System.out.print(temp1); if (m

标签: java string for-loop while-loop indexoutofboundsexception


【解决方案1】:

当你这样做时:

temp = word.replaceAll(word, "-");

...您将temp 设置为只是“-”,而不是(例如)“----”。要了解原因,请考虑 word 是否为“hello”;那么这一行看起来像:

temp = "hello".replaceAll("hello", "-");

那么稍后您假设tempword 一样长,因为您在word 中找到一个索引并尝试在temp 中访问该字符。但是temp 只有一个字符长,因此例外。

p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p));

【讨论】:

    【解决方案2】:

    试试这个..... 这将解决您的问题...!!

    包装豆类; 导入 java.util.Scanner;

    公共类刽子手{

    public static String replace(String str, int index, char replace){     
        if(str==null){
            return str;
        }else if(index<0 || index>=str.length()){
            return str;
        }
        char[] chars = str.toCharArray();
        chars[index] = replace;
        return String.valueOf(chars);       
    }
    
    public static void main (String [] args){
    
        Scanner sc = new Scanner(System.in);
    
        String word = "Sample";
        String temp = "";
        String letter = null;
    
        int n;
        int m=0;
        int p = 0;
    
        for (n = 0; n<word.length(); n++){
            temp = temp + word.replaceAll(word, "-"); //replaces the String word with "-" and prints
        }
    
        System.out.print(temp); 
    
        while (m <= 5){ //the player can only guess incorrectly 5 times
            System.out.println("\nEnter a letter:");
            letter = sc.nextLine();
    
            letter.toLowerCase();
            if (word.contains(letter) == true){
                p = word.indexOf(letter);
                temp = replace(temp, p , word.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
                System.out.println(temp);
            }
    
            else {
                System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
                m++; //to count for incorrect guesses
            }    
        }
        System.out.println("Game Over.");
    }
    

    }

    【讨论】:

    • 是的,这解决了问题!非常感谢! :)
    【解决方案3】:

    你应该检查文档中的 replaceAll() 方法。因为你用错了。

    replaceAll(字符串正则表达式,字符串替换) 用给定的替换替换此字符串中与给定正则表达式匹配的每个子字符串。

    您将整个字符串放入正则表达式参数中

    如果你这样做myString.replaceAll("\\.","-");(使用双反斜杠来指定正则表达式)将用“-”替换换行符旁边的任何字符,检查正则表达式。 Regullar expressions

    【讨论】:

    • 我尝试按照您的建议进行操作,但我仍然坚持打印直到返回索引。
    【解决方案4】:
    if (word.contains(letter) == true){
                    p = word.indexOf(letter);
                    temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
                    System.out.print(temp1);
                }
    

    word.indexOf(字母);如果后者存在于字符串中,则返回字母的索引,否则返回-1。这就是你得到异常的原因。

    【讨论】:

    • 这是错误的,它排除了单词中的最后一个索引。数组的边界是 0 &lt;= i &lt; length0 &lt;= i &lt;= length - 1
    • 是的,这并不能解决问题。它删除字符串的最后一个索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2011-05-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多