【问题标题】:Why is this a java program to display odd and even characters of a array not showing any output?为什么这是一个显示数组奇偶字符而不显示任何输出的java程序?
【发布时间】:2017-06-25 12:20:33
【问题描述】:

这是一个显示数组奇偶字符的java程序,目前没有输出:

    Scanner scan = new Scanner(System.in);
    int n=scan.nextInt();// to get the num of words
    String[] inp=new String[10000];
    char[][] imArray=new char[10][];//2d array

    for(int j=0; j<n ; j++)//to get the strings
    {
      inp [j]= scan.nextLine();
      imArray[j] = inp[j].toCharArray();
    }

    for (int j=0; j<n ; j++)
    {
      for(int i=0;  i<inp[j].length() ;i=i+2)//even chars 
        System.out.println(imArray[i]);

      for (int k=0;  k<inp[j].length() ;k=k+2) //odd chars
        System.out.println("\t"+imArray[k]);

      System.out.println("\n");
    }

这不会引发错误,但也不会显示任何输出。至少它没有显示任何运行时错误。它在主线程中显示异常。

【问题讨论】:

  • 你需要改进问题,添加代码格式
  • 是的!我是新来的,是的,我已经改进了我的问题。如果可以的话,请看看 nw。谢谢。
  • 那么例外是什么?向我们展示堆栈跟踪。
  • 回复:“改进了我的问题”。不多。我可以看到拼写错误、语法错误、标点符号错误等。更重要的是,代码缩进、空格等都是一团糟。

标签: java arrays multidimensional-array


【解决方案1】:

你的问题我不清楚。 根据你在节目中的cmets,我认为你应该试试这个:

public static void main(String args[]){

    Scanner scan = new Scanner(System.in);

    int n=scan.nextInt();// to get the number of words
    String[] inp=new String[10000];


    for(int i=0;i<=n;i++){
        inp [i]= scan.nextLine();
    }
    System.out.println("Even Values");
    for(int j=0; j<=n ; j+=2){//even chars 

        System.out.print(inp[j]+" ");
    }
    System.out.println();
    System.out.println("Odd Values");
    for(int k=1;  k<=n ;k=k+2){

        System.out.print(inp[k]+" ");
    }

        }

}

【讨论】:

    【解决方案2】:

    首先,让我们获取我们希望从用户那里获得的字数。我们允许的最大值是 10,000 字。

    Scanner scan = new Scanner(System.in);
    int wordsNumber = Math.min(Integer.parseInt(scan.nextLine()), 10000); // to get the num of words
    String[] inp = new String[wordsNumber];
    char[][] imArray=new char[wordsNumber][];//2d array
    

    现在我们从用户那里获取单词并将它们保存到数组中。

    for (int i = 0; i < wordsNumber ; i++) { //to get the strings
      inp[i] = scan.nextLine();
      imArray[i] = inp[i].toCharArray();
    }
    

    最后,迭代数组并为每个单词在偶数位置打印其字符,然后在奇数位置打印字符。 在每个单词后打印一个换行符。

    for (int i = 0; i < wordsNumber ; i++) {
      int wordLength = imArray[i].length;
    
      for(int j = 0;  j < wordLength; j+=2) { //even chars
        System.out.print(imArray[i][j]);
      }
    
      System.out.print("\t");
    
      for (int k = 1;  k < wordLength; k+=2) { //odd chars
        System.out.print(imArray[i][k]);
      }
    
      System.out.println("\n");
    }
    

    实际上,不需要使用 2D 数组,我离开是为了与问题中的代码保持相似。 最好迭代 inp 数组并使用 String.charAt(int)

    【讨论】:

    • 非常感谢!!但是你能解释一下为什么需要 Math.min 函数而不是简单地单独使用 nextInt()
    • 您帖子中的代码进行了初始化: String[] inp=new String[10000];我假设您想限制字数。如果没有,您可以放弃 Math.min 调用。
    【解决方案3】:

    首先,您要创建一个大小为 10 的数组

    char[][] imArray=new char[10][];//2d array
    

    那么你是通过分配它

    for(int j=0; j<n ; j++)//to get the strings
    {
      inp [j]= scan.nextLine();
      imArray[j] = inp[j].toCharArray();
    }
    

    这里如果n 的值大于10,,它将抛出ArrayIndexOutOfBoundsException

    imArray[j] = inp[j].toCharArray(); // <--- Here ArrayIndexOutOfBoundsException
    

    一旦发生异常,您的程序就会终止,因此不会产生任何输出。

    要解决这个问题,请声明您的 imArray 大小为 n 而不是 10

    char[][] imArray = new char[n][];
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2013-12-09
      • 2017-12-18
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多