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

时间:2014-04-09 01:53:31

标签: java

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]); 

     } 
 }

我不知道该尝试什么。任何指针都会很好。

2 个答案:

答案 0 :(得分:2)

如果我们稍微看一下......

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

您创建一个空的String,然后尝试在位置char上获取0 ...当没有任何内容时...

我猜测&#34; input假设以某种方式从用户那里获取输入,但鉴于您在此之后初始化Scanner,我只是不确定发生了什么。 ..

答案 1 :(得分:0)

char guesses = input.charAt(0); 

变量输入为空。但是你试图获得第0个索引值。