[英]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.