简体   繁体   English

我正在尝试制作一个简单的刽子手游戏,但我遇到了这个错误:字符串索引超出范围:第19行为0

[英]I'm trying to make a simple hangman game, but I run into this error: String index out of range: 0 at line 19

import java.util.Scanner; 
 public class Hangman2
 { 


 public static void main(String[] args) 
 { 

     final int LETTERCOUNT = 6; 
     char [] letters = { '_', '_', '_', '_', '_', '_'}; 
     String input = "";
     int noOfGuesses = 0, count; 
     char guesses = input.charAt(0);
     Scanner keyboard= new Scanner (System.in); 

     System.out.println ("The word has " + LETTERCOUNT + "letters"); 

     do{
         noOfGuesses = noOfGuesses+1; 
     }while (noOfGuesses > 0);

     System.out.println ("Please enter your guess"); 
     char guess = input.charAt (0); 

     for (count = 0; count <6; count ++) { 
         char letter[]={'_','_','_','_',' ','_'}; 
     }

     for(int i=0; i<letter.length;i++){ 
         if(letter[i]==' '){ // Note use ' '(' + Blank Space + ') instead of '' 
         System.out.print("Blank Space"); 
         letter[i]='M'; 
     } 

     System.out.println("(" + i + ")" + letter[i]); 

     } 
 }

I'm not sure what to try. 我不知道该尝试什么。 Any pointers would be nice. 任何指针都会很好。

If we have a little look at this... 如果我们稍微看一下......

String input = "";
int noOfGuesses = 0, count;
char guesses = input.charAt(0);

You create an empty String , then try and get the char at position 0 ...when nothing exists ... 你创建一个空的String ,然后尝试在位置0获取char ...当什么都不存在时...

I'm "guessing" that input was suppose to take input from the user in some way, but given the fact that you initialise the Scanner after this, I'm just not sure what's going on... 我在猜测input是假设以某种方式从用户那里获取输入,但考虑到你在此之后初始化Scanner这一事实,我只是不确定发生了什么......

char guesses = input.charAt(0); 

the variable input is empty. 变量输入为空。 But you are trying to get the 0th index value. 但是你试图获得第0个索引值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 openbravo 中收到此“字符串索引超出范围”3 错误 - i m getting this "String index out of range " 3 error in openbravo 我完全迷失在这个刽子手游戏中 - I'm completely lost in this hangman game 无法理解为什么我的&#39;字符串索引超出范围:-3&#39;错误-Java - Can't understand why I'm getting a 'String index out of range: -3' error - Java 当我运行程序时,出现错误java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - When I run my program i get the error java.lang.StringIndexOutOfBoundsException: String index out of range: 0 如何修复此“字符串索引超出范围错误”? - How can I fix this "String index out of range error"? 为什么出现“字符串索引超出范围:53”错误? - Why am I getting a “String index out of range: 53” error? 我的人生游戏代码中的字符串索引超出范围错误 - String index out of range error on my game of life code 我正在尝试制作一个简单的程序,用“*”符号替换&#39;a&#39;,&#39;e&#39;,&#39;i&#39;,但我收到此错误代码 - I am trying make a simple program that replaces 'a', 'e', 'i' with the “*” symbol but i'm getting this error code 我正在尝试制作一个简单的 java 处理游戏,我可以在其中从某个形状射击子弹,但它们似乎没有射击 - I'm trying to make a simple java processing game where I can shoot bullets from a shape but they don't seem to be shooting 所以,我正在尝试在 android 中做一些简单的事情,它显示了我的错误,我无法弄清楚 - So, I am trying to do simple stuff in android and it shows me error and I'm not able to figure it out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM