使用字符串charAt方法进行循环崩溃

使用字符串charAt方法进行循环崩溃

本文介绍了Java:使用字符串charAt方法进行循环崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这段代码中有一个错误。调试器建议它的原因是这行代码 char chr = getSecretWord.charAt(i);



这段代码的作用是寻找 userInput secretWord 之间的匹配。我有 for loop 逐个查看secretWord字母的长度,如果有匹配的字母返回true。如果没有,返回false ...但是当假设只返回false时程序崩溃...我想这是这行的东西,但是不知道究竟是什么 getSecretWord.charAt(i) ;

  private boolean isMatchingSecretWord(String userInput)
{
String secretWord =;
String getSecretWord = getSecretWord();
for(int i = 0; i< = getSecretWord.length(); i ++)
{
char chr = getSecretWord.charAt(i);
secretWord =+ chr;

if(secretWord.equals(userInput))
{
println(is true);
返回true;
}
}
返回false;
}

作为旁注,我对此代码所做的是正确的,将getSecretWorld()方法赋给String,这样我就可以使用Strings方法 length()



String getSecretWord = getSecretWord();



for(int i = 0; i< = getSecretWord.length(); i ++)



调试代码:

 线程Thread-4中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 
at java.lang.String.charAt(String.java:686)
at Hangman.isMatchingSecretWord(Hangman.java:49)
at Hangman.userInput(Hangman.java:34 )
在Hangman.run(Hangman.java:20)*


解决方案
  for(int i = 0; i< = getSecretWord.length(); i ++)
// ^^^
//见这里

n -character字符串(或 n -element数组)的有效索引是 0 n-1 包含。



所以,如果您的密码是 xyyzy ,有效索引为0到4。你的原始循环迭代, i 通过 5设置为零,因此问题。






但是那里似乎有很多不必要的代码,当你可以逃避简单的事情时。



首先,我会删除一个混乱的来源 - 函数名称听起来像用户输入和秘密字必须完全匹配,而你的评论指示否则:

在这种情况下,你只想查看密码中是否存在单个字符。我会更改函数名称以适应,即使这样,也可以使用更少的代码完成:

  private boolean isInSecretWord (String userInput){
String secretWord = getSecretWord();
返回secretWord.contains(userInput);
}


I have a bug in this block of code. The debugger suggest it´s cause is this line of code char chr = getSecretWord.charAt(i);

What this code does is look for a match between userInput and secretWord. I have the for loop to go through the length of the secretWord letters one by one, and if there is a letter matching return true. If not, return false... but the program crashes when it is suppose to just return false... I guess it is something with this line, but do not know exactly what getSecretWord.charAt(i);

    private boolean isMatchingSecretWord(String userInput)
{
    String secretWord = "";
    String getSecretWord = getSecretWord();
    for (int i = 0; i <= getSecretWord.length();i++)
        {
        char chr = getSecretWord.charAt(i);
        secretWord = ""+chr;

        if (secretWord.equals(userInput))
        {
            println("is true");
            return true;
        }
    }
    return false;
}

As an side note, is what I´ve done with this code correct, assigning the getSecretWorld() Method to a String so I can use the Strings method length()?

String getSecretWord = getSecretWord();

for (int i = 0; i <= getSecretWord.length();i++)

Debug code:

Exception in thread "Thread-4" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(String.java:686)
    at Hangman.isMatchingSecretWord(Hangman.java:49)
    at Hangman.userInput(Hangman.java:34)
    at Hangman.run(Hangman.java:20)*
解决方案
for (int i = 0; i <= getSecretWord.length(); i++)

should be:

for (int i = 0; i < getSecretWord.length(); i++)
//               ^^^
//             see here

The valid indexes for an n-character string (or an n-element array) are 0 through n-1 inclusive.

So, if your secret word is xyyzy, the valid indexes are zero through four. Your original loop iterates with i set to zero through five, hence the problem.


But there seems to be a lot of unnecessary code in there, when you could get away with something simple.

First, I would remove a source of confusion - the function name sounds like the user input and the secret word have to match completely whereas your comment indicates otherwise:

In that case, you simply want to see if the single character exists in the secret word. I would change the function name to suit and, even then, it can be done with a lot less code:

private boolean isInSecretWord (String userInput) {
    String secretWord = getSecretWord();
    return secretWord.contains(userInput);
}

这篇关于Java:使用字符串charAt方法进行循环崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:31