Why the console said Index 0 out of bounds for length 0?
It says in my method CharacterSearch().

这是我的代码.顺便说一下,代码是Hangman Game.我对Java是个新手,并不了解所有的库.

package besingaPi_LE_3_2;

import java.util.Scanner;

public class GameHangman {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int miss = 0; 
        String choice1;
        String temp;
        
        String[] words = {"giraffe", "hipopotamus", "gorilla", "peacock", "armadillo"};

        int choice;
        //do {
            mainMenu();
            choice = input.nextInt();
            System.out.println();
            if(choice == 1){
                
                String wordRandom = Randomizer(words);
//              char[] letters = new char[wordRandom.length()];
                String mute  = secretWord(wordRandom);
                temp = mute;
//              char c = wordRandom.charAt(3);
//              System.out.println(c);
                System.out.println(wordRandom);
                System.out.println("Note: use only small character/s");
                
                while(!mute.equals(wordRandom)) {
                    System.out.print("(Guess) Enter a letter in word " + mute + " > ");         
                    choice1 = input.nextLine();
//                  char letter = choice1.charAt(0);
                    mute = CharacterSearch(wordRandom, mute, choice1);

                    if(temp.equals(mute)) {
                        miss++;
                    }
                    temp = mute;
    
                }
                System.out.println("The word is " + wordRandom + "." + " You missed " + miss + " time.");
            }
            else if (choice == 0) {
                System.out.println("Thank you!!");
                return;
            }

    }
    public static void mainMenu() {
        System.out.println("Welcome to Hangman");  
        System.out.println("[1]START\n[0]EXIT"); 
        System.out.print("Please input your choice: ");
        return; 
    }
    public static String CharacterSearch(String word, String muted, String key) {
        StringBuilder update = new StringBuilder(muted);
        int i;
        for(i = 0; i < word.length(); i++) {
            if(word.charAt(i)  == key.charAt(0)) {
                update.setCharAt(i,key.charAt(0));
            }
        }
        
        muted = update.toString();
        return muted;
    }
    public static String secretWord(String word) {
        String temp = "";
        for(int i = 0; i < word.length(); i++) {
            temp += "*";
        }
        return temp;
    }
    
    public static String Randomizer(String[] words) {
        int rand = (int)(Math.random() * words.length);
        String word = words[rand];
        return word;
    }
}

我try 了这么多东西,但它总是会说Index Out Out.

推荐答案

您遇到的"索引0超出长度为0的界限"错误可能是因为在使用input.nextInt()读取整数输入后调用了input.nextLine().当您在输入整数后按Enter键时,input.nextLine()将读取代码未捕获的换行符(‘\n’).

要解决此问题,您可以在读取整数后额外添加input.nextLine()以使用换行符.如下所示修改代码:

choice = input.nextInt();
input.nextLine();
System.out.println();
if (choice == 1) {
    // ...
}

这应该可以防止现在的错误.别担心,我以前经常犯这样的错误.

Java相关问答推荐

屏蔽字母数字代码的Java正则表达式

JDK22执行repackage of goal org. springframework. boot:spring—boot—maven—plugin:3.2.3:repackage failed:unsupported class file major version 66—>

Hibernate 6支持Joda DateTime吗?

Java中如何根据Font.canDisplay方法对字符串进行分段

连接Quarkus中的两个异步操作

try 创建一个对象,使用它,然后使用一条语句将其存储为列表

是否在允许数组元素为空时阻止 idea 为空性警告?

在Java Swing Paint应用程序中捕获快速鼠标移动时遇到困难

对Java中的通配符参数的混淆

SpringBoot+Java 17@Valid未验证POJO

类型集合的Jackson JsonNode:类型引用的对象读取器应该是Singleton吗?

为什么当我创建Robot对象时,JavaFX引发IlLegalStateException异常?

为什么有两种实现来检索数组类的组件类型?

Android Java:已设置但未读取SharedPreferences

垃圾回收器是否真的删除超出作用域的对象?

寻找Thread.sky()方法的清晰度

Java.time.OffsetDateTime的SQL Server数据库列类型是什么?

如何从命令行编译包中的所有类?

在打开搜索结果时,如何让Eclipse打开整个文件?

如何设置默认序列生成器分配大小