import java.io.File;
import java.util.*;
import javax.swing.JOptionPane;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
Random ran = new Random();
Scanner file = new Scanner(new File("word.txt"));
String dictionary = file.nextLine();
char array2[];
array2 = new char[7];
String wordlist[] = {"accoutrements","acumen","anomalistic","auspicious","bellwether","callipygian","circumlocution"};
int number = ran.nextInt(3);
String in = null;
in = wordlist[number];
//System.out.println(number);
String guess;
int numofchances = 8,k=0;
boolean array[],b=true;
array = new boolean[in.length()];
for (int u=0;u<in.length();u++)
{
array[u]=false;
}
while(numofchances >= 1 && b){
numofchances--;
System.out.println("Enter your guess: ");
guess = input.nextLine();
char character = guess.charAt(0);
array2[k] = character;
k++;
for(int i=0;i<in.length();i++){
char c = in.charAt(i);
if(c == character || array[i]==true){
System.out.print(c+" ");
array[i] = true;
}else{
System.out.print("_ ");
}
}
System.out.println();
for(int i=0;i<k;i++){
System.out.print(array2[i]);
}
System.out.println();
b = false;
System.out.println();
for (int i=0;i<in.length();i++){
if (array[i]==false)
b=true;
}
}
}
}
你如何解决这个错误?每当我输入超过 7 个字母时,就会在“array2[k] = character;”这一行出现此错误
输出:输入你的猜测:a a _ _ _ _ _ a
输入您的猜测: b a _ _ _ _ _ ab
输入您的猜测: f a _ _ _ _ _ abf
输入你的猜测:d a _ _ _ _ _ abfd
输入您的猜测: f a _ _ _ _ _ abfdf
输入您的猜测:r a _ _ _ _ _ abfdfr
输入您的猜测: f a _ _ _ _ _ abfdfrf
输入您的猜测: f 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 at Hangman1.main(Hangman1.java:35)