【问题标题】:Why am I getting an error for index 0 out of bounds for length 0 when trying to print separate rows of stars?为什么在尝试打印单独的星形行时,索引 0 超出长度 0 的范围出现错误?
【发布时间】:2020-09-26 01:56:12
【问题描述】:

我希望它可以打印星星。结果是它以我想要的方式打印了星星,但是在测试时出现错误,“ArrayIndexOutOfBoundsException:索引 0 超出长度 0 的范围”。 代码:

public class Printer {

    public static void main(String[] args) {
        // You can test the method here
        int[] array = {5, 1, 3, 4, 2};
        printArrayInStars(array);
   }

    public static void printArrayInStars(int[] array) {
        // Write some code in here
   
         int i = 1;
        while (i<=array[0]) {
            System.out.print("*");
            i++;           
        }
        System.out.println("");
    
        int a = 1;
        while (a<=array[1]) {
            System.out.print("*");
            a++;           
        }
        System.out.println("");
    
        int b = 1;
        while (b<=array[2]) {
            System.out.print("*");
            b++;           
        }
        System.out.println("");
    
        int c = 1;
        while (c<=array[3]) {
            System.out.print("*");
            c++;           
        }
        System.out.println("");

        int d = 1;
        while (d<=array[4]) {
            System.out.print("*");
            d++;           
        }
        System.out.println("");
    }
}

结果:




**

【问题讨论】:

  • 可能需要重新安排循环的方式。
  • 您的代码在我这边运行良好。我得到以下输出 - ***** * *** **** **
  • 你能告诉我们更多你是如何测试你的代码的细节吗?
  • @Numery 我正在使用 tmc bean,当我单击运行项目时,它给了我正确的输出,但是当我单击本地运行测试时,我得到了那个错误。
  • 请向我们提供更多信息。您的测试文件中有什么代码?可以查一下吗?

标签: java


【解决方案1】:

这是一种循环数组并打印 array[i] 数量 * 的简单方法,还有更好的方法,但这可能是最简单的方法。

public static void main(String[] args) {
    // You can test the method here
    int[] array = {5, 1, 3, 4, 2};
    for(int i=0;i<array.length;i++)
    {
        printArrayInStars(array[i]);
    }
}

public static void printArrayInStars(int n) {
    // Write some code in here
    for(int i=0;i<n;i++)
    {
        System.out.print("*");
    }
    System.out.println("");
}

输出

*****
*
***
****
**

【讨论】:

    【解决方案2】:

    伙计这是图片,我发布的你可以看到没有运行时错误, 您可能会更改编译器,或者复制并粘贴到新的 java 文件并再次运行它

    【讨论】:

      【解决方案3】:
      class star
      {
          public void series()
          {
              String [] a = {"*","*","*","*"};
              for(int i=0; i<4; i++)
              {
                  for(int j=0; j<4;j++)
                  {   
                      System.out.printf(a[j]);
                  }System.out.printf("\n");
              }
          }
          public static void main(String [] args)
          {
              star s = new star();
              s.series();
          }
      }
      

      输出将是 4行星, 每行将有 4 颗星

      如果有其他期待,请清除您的问题。 n 你想用什么方式打印星星。

      【讨论】:

      • 这不能回答问题。停止直接发布解决方案。相反,请考虑解释。
      • 好哥们,我尝试运行你的程序,没有错误。
      • 问题是Why am I getting an error for index 0 out of bounds for length 0 when trying to print separate rows of stars?。您的回答几乎没有试图解释原因,output will be 4 line of stars, each line will have 4 stars if expect something else, please clear your question. n what manner you wanna print stars 并没有真正的帮助。
      • 好的哥们,如果你没有程序问题,你来这里就知道为什么会出现越界错误。然后很简单,您的程序没有错误,您可以在新答案中看到。当您尝试获取或输入数组中的值超过定义的大小时,会出现越界。您没有尝试从数组中获取值,因此不可能出错。您只是将数组的值与不同变量的值进行比较。我很抱歉我第一次没有得到你的问题。在图片中,输出是您的程序的输出,没有任何修改。
      • 当我运行代码时它可以工作,但是当我测试它时,我得到了那个错误。它说索引 0 超出范围,但数组的索引 0 处有一个值,所以我很困惑为什么会出现错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多