Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误消息Java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0_Java - Fatal编程技术网

Java错误消息Java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

Java错误消息Java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0,java,Java,这是我的节目 import java.util.Scanner; import java.util.Random; public class Project3 { public static void main(String[] args) { int low; int high; int answer; int guess; int numGuess = 0; int x;

这是我的节目

import java.util.Scanner;
import java.util.Random;

public class Project3
{
    public static void main(String[] args)
    {

        int low;
        int high;
        int answer;
        int guess;
        int numGuess = 0;
        int x;
        int y;

       char repeat; // this will hold y or n 
       String input; //holds input to perform entire loop


        System.out.println( "Hello and welcome to Guess That Number!");

            Scanner keyboard = new Scanner(System.in);

        System.out.println("For starters you get to pick the range the number falls in!" +
                            " HOW EXCITING!");


        System.out.println( "Now what would you like the lowest possible number to be?");
                      low = keyboard.nextInt();

        System.out.println( "and the highest?");
                      high = keyboard.nextInt();
 do  
  {    Random randomNumber = new Random();

        answer = randomNumber.nextInt();
       while (answer < low || answer > high)
       { 
        answer = randomNumber.nextInt();
    }



       guess = -1;

       while(guess != answer)
       {

           System.out.println("What is your guess?");
          System.out.println("Don't forget has to be in between "+ low + " and " + high);

                   guess = keyboard.nextInt();

                   numGuess = (numGuess + 1);

                if (guess < answer)
                {
                    System.out.println("TOO LOW!");

                }

                else if (guess > answer)
                {
                    System.out.println("TOO HIGH!");



                }


            }





       System.out.println("YOU GOT IT WOOOO!");
       System.out.println("The number was " + answer);
       System.out.println("Nice it only took " + numGuess + "!");

       for ( x = 1; x <= numGuess; x++)

      {

          for ( y = 1; y <= answer; y++)

            {
                System.out.print("*");
            }

            System.out.println();



      }


      System.out.println("\nWould you like to play again? \n" +        // this is to loop the entire game
                      "Enter Y for yes or N for no. \n");

                      input = keyboard.nextLine();
                      repeat = input.charAt(0);

  } while (repeat == 'Y' || repeat == 'y');

    if (repeat == 'n' || repeat == 'N')             
    {

    System.out.println("\nThanks for playing! \n");

   }
}
}
import java.util.Scanner;
导入java.util.Random;
公共类项目3
{
公共静态void main(字符串[]args)
{
int低;
int高;
int答案;
智力猜测;
int numGuess=0;
int x;
int-y;
char repeat;//这将保存y或n
字符串输入;//保存输入以执行整个循环
System.out.println(“您好,欢迎猜那个数字!”);
扫描仪键盘=新扫描仪(System.in);
System.out.println(“对于初学者,您可以选择数字所属的范围!”+
“多么令人兴奋!”;
System.out.println(“现在您希望最低可能的数字是多少?”);
low=键盘.nextInt();
System.out.println(“最高的是什么?”);
high=键盘.nextInt();
做
{Random randomNumber=新随机数();
答案=randomNumber.nextInt();
同时(回答<低| |回答>高)
{ 
答案=randomNumber.nextInt();
}
猜测=-1;
while(猜!=回答)
{
System.out.println(“你猜怎么着?”);
System.out.println(“别忘了必须在“+低+”和“+高”之间);
guess=keyboard.nextInt();
numGuess=(numGuess+1);
如果(猜测<回答)
{
System.out.println(“太低了!”);
}
else if(猜测>回答)
{
System.out.println(“太高了!”);
}
}
System.out.println(“你明白了!”;
System.out.println(“数字是”+答案);
System.out.println(“不错,只花了“+numGuess+”!”;

对于(x=1;x执行此操作之前:
repeat=input.charAt(0);


检查
字符串是否至少有
一个字符。

执行此操作之前:
重复=输入.charAt(0);


检查
字符串是否至少有
一个字符。

IndexOutOfBoundsException表示您试图访问的数组索引不存在。异常显示您在第687行遇到错误,是字符串.charAt()方法。我建议您仔细查看该行附近的代码。如果您使用的是IDE,则应尝试在调试中使用该行附近的断点执行,并逐行遍历代码以查看变量。

IndexOutOfBoundsException意味着您尝试访问的数组索引不存在。异常tion显示它在第687行遇到错误,是字符串.charAt()方法。我建议您仔细查看该行周围的代码。如果您使用的是IDE,您应该尝试在调试中使用该行附近的断点执行,并逐行查看代码以查看变量。

首先,请注意,如上所述,如果没有输入,则您的
while
条件将为false
'Y'
'Y'
退出,最终的
“\n播放等级!\n”
将永远不会显示。您应该修改此结构。可能类似于:

boolean playing = true;

while (playing) {
   // play game here

   // ask to play again?

   if (answer == 'N') {
      playing = false;
   }
}

System.out.println("\nThank you for playing!\n");

现在,为了解决你原来的问题,你不检查空输入。错误很可能只发生在你输入没有输入任何东西的时候。另一个问题是,如何处理空值?它被认为是“代码>‘N’/代码>或代码>y’< /代码>?如果你认为空值是一个有效的默认选择,你是首。ld在显示的问题中指出它。类似于:

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No (default Yes) : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty, we default the value
      repeat = 'Y';     // default value, change this to 'N' if default is No
   } else {
      repeat = input.charAt(0);
      if (repeat != 'N' && repeat != 'Y') {
         System.out.print("Ooops! Please enter Yes or No :");
         repeat = '\0';
      }
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase
如果不需要默认值,只需将构造更改为

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty...
      repeat = '\0';     // null character for empty value
   } else {
      repeat = input.charAt(0);
   }
   if (repeat != 'N' && repeat != 'Y') {
      System.out.print("Ooops! Please enter Yes or No :");
      repeat = '\0';     // make sure the character is null so we don't exit the loop yet
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase

首先,请注意,如上所述,如果任何输入不是
'Y'
'Y'
,则您的
while
条件将为false,因此退出,最终的
“\n播放等级!\n”
将永远不会显示。您应该修改此结构。可能类似于:

boolean playing = true;

while (playing) {
   // play game here

   // ask to play again?

   if (answer == 'N') {
      playing = false;
   }
}

System.out.println("\nThank you for playing!\n");

现在,为了解决你原来的问题,你不检查空输入。错误很可能只发生在你输入没有输入任何东西的时候。另一个问题是,如何处理空值?它被认为是“代码>‘N’/代码>或代码>y’< /代码>?如果你认为空值是一个有效的默认选择,你是首。ld在显示的问题中指出它。类似于:

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No (default Yes) : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty, we default the value
      repeat = 'Y';     // default value, change this to 'N' if default is No
   } else {
      repeat = input.charAt(0);
      if (repeat != 'N' && repeat != 'Y') {
         System.out.print("Ooops! Please enter Yes or No :");
         repeat = '\0';
      }
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase
如果不需要默认值,只需将构造更改为

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty...
      repeat = '\0';     // null character for empty value
   } else {
      repeat = input.charAt(0);
   }
   if (repeat != 'N' && repeat != 'Y') {
      System.out.print("Ooops! Please enter Yes or No :");
      repeat = '\0';     // make sure the character is null so we don't exit the loop yet
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase
与此相反:

repeat = input.charAt(0);
这样做:

try
{
    repeat = input.charAt(0);
}
catch (java.lang.StringIndexOutOfBoundsException exception)
{
//Solve the problem
}
这样,您可以捕获异常并处理它。 编辑:最好在第一课中学习一点Java异常处理,因为它并不复杂,而且在以后更困难的任务中会很有用。

而不是:

repeat = input.charAt(0);
这样做:

try
{
    repeat = input.charAt(0);
}
catch (java.lang.StringIndexOutOfBoundsException exception)
{
//Solve the problem
}
这样,您可以捕获异常并处理它。
编辑:最好在第一课中学习Java中的一点异常处理,因为它并不复杂,在以后更困难的任务中会很有用。

用户似乎没有输入
Y
N
就按下了
return/enter

这是一个建议。毫无疑问,代码可以在许多方面做得更好,但这只是针对这个问题的一个建议。完全去掉
repeat
变量,并用它们替换相应的行

do {
    .....

} while ("y".equalsIgnoreCase(input));

if (!"y".equalsIgnoreCase(input)) {
  System.out.println("\nThanks for playing! \n");
}

用户似乎正在按
return/enter
,而没有输入
Y
N

这是一个建议。毫无疑问,代码可以在许多方面做得更好,但这只是针对这个问题的一个建议。完全去掉
repeat
变量,并用它们替换相应的行

do {
    .....

} while ("y".equalsIgnoreCase(input));

if (!"y".equalsIgnoreCase(input)) {
  System.out.println("\nThanks for playing! \n");
}

与问题不完全相关,但如果输入y或n以外的任何内容,程序将不带任何提示退出