Java 索引自动边界错误,刽子手游戏

Java 索引自动边界错误,刽子手游戏,java,Java,我正在做一个快速的刽子手游戏,遇到了一个indexootfboundsexception,我想知道为什么。我看不出这个错误是怎么发生的 它发生在这一行: array[index]+=c; 任何反馈都将不胜感激 import java.io.IOException; import java.util.Scanner; public class Driver { public static void main(String[]args) throws IOException {

我正在做一个快速的刽子手游戏,遇到了一个
indexootfboundsexception
,我想知道为什么。我看不出这个错误是怎么发生的

它发生在这一行:

array[index]+=c;
任何反馈都将不胜感激

import java.io.IOException;
import java.util.Scanner;

public class Driver {

    public static void main(String[]args) throws IOException {

        Scanner console = new Scanner (System.in);
        String[] phrase={"television"};
        String[] array= new String[phrase.length];
        int body =6;
        while(array!=phrase) {
            char c=(char)System.in.read();
            int index= console.nextInt();
            array[index]+=c;
            if(array[index].charAt(index)==phrase[index].charAt(index)){
                System.out.println("the new array");
            }
        }
    }
}

代码有很多问题。他们中很少有人在下面

  • 您正在创建大小为“phrase.length”的数组,当我为“console.nextInt();”输入2时,该数组的大小将为1,它将使索引超出范围
  • 数组相等性检查错误,需要执行以下操作

    if(Arrays.equals(array1,array2))


该代码存在许多问题。他们中很少有人在下面

  • 您正在创建大小为“phrase.length”的数组,当我为“console.nextInt();”输入2时,该数组的大小将为1,它将使索引超出范围
  • 数组相等性检查错误,需要执行以下操作

    if(Arrays.equals(array1,array2))


@Jens这里声明:
int index=console.nextInt(),问题是所提供的输入是什么。请发布并包含完整的堆栈跟踪。@Ori Lentz你是什么意思?处理数组的方式有很多问题,我认为您创建数组的方式与预期不同。请查阅此基础教程:。它应该清除一些内容来清除这些内容。用户键入一个字符,按enter键,然后键入一个数字,表示他们要将字母放在的位置。这个数字将表示数组中的一个in索引。@Jens它在这里声明:
int index=console.nextInt(),问题是所提供的输入是什么。请发布并包含完整的堆栈跟踪。@Ori Lentz你是什么意思?处理数组的方式有很多问题,我认为您创建数组的方式与预期不同。请查阅此基础教程:。它应该清除一些内容来清除这些内容。用户键入一个字符,按enter键,然后键入一个数字,表示他们要将字母放在的位置。这个数字将代表数组中的索引。天哪,你真是个天才,谢谢你!!!天哪,你真是个天才,多谢了!!!