【问题标题】:Need help fixing the while loop需要帮助修复 while 循环
【发布时间】:2015-09-13 09:42:08
【问题描述】:

您好,我试图创建一个模拟数据库搜索,尽管它有效,但每当我输入不属于数据库的输入时,它会在线程“main”java.lang.ArrayIndexOutOfBoundsException: 4 on line 中创建一个异常23. 我不知道还能做什么,因为我没有看到代码中有错误。

import java.util.*;

public class Database {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = new Scanner(System.in);
    String[] names = new String[4];

    boolean found = false;
    int i = 0;
    names[0] = "Thor";
    names[1] = "Ben";
    names[2] = "Zoe";
    names[3] = "Kate";

    System.out.println("Enter Player Name");
    String input = scan.nextLine();

    while(found != true){

        if(input.equals(names[i])){
            System.out.println(input + " has been found");
            found = true;
        } else {
            i = i+1;
        }
        if(i == 3 && found == false){
            System.out.println(input + " was not found");
        }

      }

    }

  }

【问题讨论】:

  • 顺便说一句,您可以使用i++(而不是i = i + 1)和!found(而不是found == false);

标签: java string while-loop int


【解决方案1】:

打印input + " was not found" 后,您不会离开循环。 因此循环的下一次迭代抛出ArrayIndexOutOfBoundsException

您应该在完成整个数组的测试后离开循环。

改变

    while(found != true){

    while(!found && i < names.length){

其实你可以把测试输入是否在循环之后的 if 语句移动:

  while(!found && i < names.length) {
    if(input.equals(names[i])){
        System.out.println(input + " has been found");
        found = true;
    } else {
        i = i+1;
    }
  }
  if(!found){
      System.out.println(input + " was not found");
  }

更好的选择是使用 for 循环:

  for (int i = 0; i < names.length && !found; i++) {
    if(input.equals(names[i])){
        System.out.println(input + " has been found");
        found = true;
    }
  }
  if(!found){
      System.out.println(input + " was not found");
  }

【讨论】:

  • 谢谢!它绝对是一种享受。对于我的完整数据库,我会牢记这一点。这是一种测试所需概念的实践。
【解决方案2】:

如果您的输入与 i 的值不匹配,则将继续递增并且您的数组长度为 4。显然 ArrayindexoutofException。 为避免您还需要考虑数组长度。

【讨论】:

    【解决方案3】:

    将您的 while 循环更改为

        while (found != true) {
    
            if (input.equals(names[i])) {
                System.out.println(input + " has been found");
                found = true;
            } else {
                i = i + 1;
            }
            if (i == 4 && found == false) {   //changed here
    
                System.out.println(input + " was not found");
                //or found == true;
                break;           //and here
            }
    
        }
    

    如果这个条件为真,你需要退出循环

    i == 4 && found == false
    

    要真正退出,你必须“打破”while 条件

    found != true
    

    您可以通过设置found=true(但这在语义上不正确)或添加break 指令来做到这一点。

    这是您的 while 循环的替代解决方案:

        while (!found && i<4) 
            if (input.equals(names[i++]))found = true;
        System.out.println(input+(found?" has been":" was not")+" found");
    

    【讨论】:

      【解决方案4】:

      你可以简单地改成

      while(i < names.length)
      

      忘记额外的布尔变量。由于您想继续迭代 i 直到找到解决方案,所以停止条件将是最大 i。当你找到你的解决方案时,你可以简单地breakthe while 语句:

      if (input.equals(names[i])) {
          System.out.println(input + " has been found");
          break;
      } 
      

      【讨论】:

        猜你喜欢
        • 2013-05-24
        • 1970-01-01
        • 2023-02-19
        • 1970-01-01
        • 2016-11-30
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多