Java 按下按钮时字符串索引超出范围

Java 按下按钮时字符串索引超出范围,java,Java,我正在做的一个小刽子手游戏有问题 private void btnTActionPerformed(java.awt.event.ActionEvent evt) { btnT.setEnabled(false); if(word.charAt(0)=='t'){ lblSpace1.setText("T"); } if(word.charAt(1)==

我正在做的一个小刽子手游戏有问题

private void btnTActionPerformed(java.awt.event.ActionEvent evt) 
{                                     
    btnT.setEnabled(false);          
    if(word.charAt(0)=='t'){
        lblSpace1.setText("T");
    }
    if(word.charAt(1)=='t'){
        lblSpace2.setText("T");
    }
    if(word.charAt(2)=='t'){
        lblSpace3.setText("T");
    }        
    if(word.charAt(3)=='t'){
        lblSpace4.setText("T");
    }        
    if(word.charAt(4)=='t'){
        lblSpace5.setText("T");
    }        
    if(word.charAt(5)=='t'){
        lblSpace6.setText("T");
    }        
    if(word.charAt(6)=='t'){
        lblSpace7.setText("T");
    }        
    if(word.charAt(7)=='t'){
        lblSpace8.setText("T");
    }        
    if(word.charAt(8)=='t'){
        lblSpace9.setText("T");
    }        
    if(word.charAt(9)=='t'){
        lblSpace10.setText("T");
    }
    if(!word.contains("t")){
        guessesLeft--;
        lblGuessesleft.setText("Number: "+guessesLeft+"");            
    }
}
代码检查字母
t
是否在单词的每个空格中。但是,有些单词的长度不是10个字符,因此会打印错误“字符串索引超出范围”


有没有办法只检查
word
中的字母?

您可以简单地执行以下操作:

@Override
public void actionPerformed(ActionEvent e)
{

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

        if(word.charAt(i) == 't')//if this condition is true
        {                  //that means the character is at the position `i`
            switch (i)
            {
                case 1:
                    lblSpace1.setText("T");
                    break;
                case 2:
                    // do other stuff
                    break;
                default:
                    break;
            }
        }
}
@覆盖
已执行的公共无效操作(操作事件e)
{
for(int i=0;i

使用此代码不能出现类型
IndexOutOfRange
的问题。

我鼓励您阅读一些基本数据结构,如数组以及
while
循环的基本
。这将极大地帮助你在游戏中工作

以下是从何处开始的想法:

int word_length = word.length();
char input = 't'; //instead of hard coding t, try getting the user input from somewhere


for( int i=0; i< word_length; i++ )
{
    if( word.charAt(i) == input )
    {
        //do stuff with labels
    }
}
int word_length=word.length();
字符输入='t'//不要硬编码t,而是尝试从某处获取用户输入
for(int i=0;i
您可能想看看的另一种口味是
Java8
中,您可以这样做

public void actionPerformed(ActionEvent e)
{
    String s = "TTTTT";
       List<String> list = Arrays.asList(s.split(""));
       list.stream()
           .filter( st -> st.equals("T"))
           .forEach(st -> System.out.println(" I am T"));

   IntStream.range(0, list.size())
             .filter(index -> list.get(index).equals("T"))
             .forEach(index -> System.out.println("The poistion is " + index));
}
解释

字符串s通过使用split和regex“”转换为列表,列表使用具有类似于if子句的筛选器的流类,并且字符串st等于T;每一个都是一个for-loop系统


第二个答案类似于for loop,因此您可以访问索引及其项,听起来您需要了解和。您能给我们提供更多信息吗?什么是<代码> LBLISESPE10?考虑提供一个演示您的问题的A。这将减少混乱,提高效率responses@MadProgrammer我张贴了我的答案。我觉得它也很破旧。我不知道为什么?很高兴听到你的意见,谢谢you@KickButtowski我首先关心的是Lambda函数的使用,糟糕的OP似乎不了解数组,更不用说集合了,这可能太过分了-IMHOAh,这将大大减少不必要的代码。然而,这并没有找到字母“t”在单词中出现的位置?我在问题中使用的笨拙代码就是这样做的。我不确定我是否真的理解。如何让它告诉我字符的位置?@L0glamb I是您可以使用charAt访问字符串字符的索引(I)基于流的解决方案将是一个很好的“高级”答案,@Kick Buttowski。看起来您的筛选器不正确,而且
lblSpace*
变量需要筛选器的位置才能执行操作。@JanNielsen谢谢。我的过滤器怎么了?你能说清楚点吗?因为它在我的笔记本电脑里运行得很好谢谢again@JanNielsen在我的第二个答案中,OP可以访问索引及其项。
 I am T
 I am T
 I am T
 I am T
 I am T
The poistion is 0
The poistion is 1
The poistion is 2
The poistion is 3
The poistion is 4