【问题标题】:counter not indicating as expected 2D array计数器未按预期指示二维数组
【发布时间】:2015-02-24 23:58:46
【问题描述】:

我的程序应该计算数组中最长的行以及最长的行在哪一行。我的计数停滞不前,我的眼睛因试图找到我的 OBO(减一)错误而受到伤害。请帮忙:)谢谢!方法错误horizontalPath

public class game {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         // TODO code application logic here
        Scanner kbd = new Scanner(System.in);
        System.out.println("ENTER A SINGLE INTEGER: ");
        int n = kbd.nextInt();
        char[][] mazeValue = new char[n+2][n+2];
        System.out.println("ENTER A PATH: ");
        for(int i = 0; i < mazeValue.length; i++ ){
            for(int j = 0; j< mazeValue[i].length; j++){
                if (i==0 || j==0 || i==n+1 || j == n+1) mazeValue[i][j] = 'X'; 
                else mazeValue[i][j]= kbd.next().charAt(0);
        }
     }
    printMaze(mazeValue);
    horizontalPath(mazeValue,n);
  }
    public static void printMaze(char newArray[][])
    {
        System.out.println("MAZE");
        for(int i = 0; i < newArray.length-2; i ++)
     {
            for (int j = 0; j < newArray[i].length-2; j++)
            {
            System.out.printf("%5c",newArray[i+1][j+1]);
            }
         System.out.printf("\n");
     }
   }
   public static void horizontalPath(char mazeValue[][], int n)
   {
       int[] totalRow = new int[n];
       
       //int horizontalPath=0;
       int count=0;
       int i;
       int j;
       for(i= 0; i<mazeValue.length-2; i++){
           for(j = 0; j<mazeValue[i].length-2; j++){
                if(mazeValue[i][j]== 'O'){
                 count++;
           }
           else{
               if(totalRow[i] < count)
                   totalRow[i]=count;
              count = 0;
           }          
        }
        if(totalRow[i] < count)
                totalRow[i]=count;
        count = 0;
    }
           int biggestRow = totalRow[0];
           //int longestRow=0;
           int finalLongestRow =0;
           for(int x =0; x <n; x++){
               if(biggestRow < totalRow[x]){
                   biggestRow = totalRow[x];
                   finalLongestRow  = x;
            }
       }
       System.out.printf("Longest horizontal path row %d length %d",finalLongestRow+1,biggestRow);

   }

【问题讨论】:

  • 问题可能是由于大括号不正确。尝试正确格式化您的代码(使用工具),看看事情是否变得更清晰......
  • 这就是问题所在???我的意思是我检查了我的大括号我的逻辑我觉得他们应该没问题
  • 不,但它可能是您在询问之前应该排除的错误来源...i = 0; i &lt; mazeValue.length - 2; i++ -> 您可能的意思是 i = 1?!

标签: java arrays for-loop counter


【解决方案1】:

在您的主目录中,System.out.println("ENTER A PATH: "); 之后,在您的 for 循环中,它正在等待用户输入,但不再要求路径。将 System.out.println("ENTER A PATH: "); 移动到你的 else 中。


编辑

这是我从你的代码中得到的:

/**
 * @param args
 *            the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    // TODO code application logic here
    Scanner kbd = new Scanner(System.in);
    System.out.println("ENTER A SINGLE INTEGER: ");
    int n = kbd.nextInt();
    char[][] mazeValue = new char[n + 1][n + 1];
    for (int i = 0; i < mazeValue.length; i++) {
        for (int j = 0; j < mazeValue[i].length; j++) {
            if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
                mazeValue[i][j] = 'X';
            else {
                System.out.println("ENTER A PATH: ");
                // You know there are better ways... you are throwing away the entire line -- is that intended?
                mazeValue[i][j] = kbd.next().charAt(0);
            }
        }
    }
    printMaze(mazeValue);
    horizontalPath(mazeValue, n);
}

public static void printMaze(char newArray[][]) {
    System.out.println("MAZE");
    for (int i = 1; i < newArray.length; i++) {
        for (int j = 1; j < newArray[i].length; j++) {
            System.out.printf("%5c", newArray[i][j]);
        }
        System.out.printf("\n");
    }
}

public static void horizontalPath(char mazeValue[][], int n) {
    int[] totalRow = new int[n];

    // int horizontalPath=0;
    int count = 0;
    for (int i = 1; i < mazeValue.length; i++) {
        for (int j = 1; j < mazeValue[i].length; j++) {
            if (mazeValue[i][j] == 'O') {
                count++;
            }
        }
        if (totalRow[i - 1] < count)
            totalRow[i - 1] = count;
        count = 0;
    }
    int biggestRow = totalRow[0];
    // int longestRow=0;
    int finalLongestRow = 0;
    for (int x = 0; x < n; x++) {
        if (biggestRow < totalRow[x]) {
            biggestRow = totalRow[x];
            finalLongestRow = x;
        }
    }
    System.out.printf("Longest horizontal path row %d length %d", finalLongestRow + 1, biggestRow);
}

所以,有一些事情需要注意——我们在每个维度(x 和 y)上加一个。这是因为您的第一行是边框,而您的第一列是边框。因此,在迭代时,我们的 i 和 j 都从 1 开始。

现在,记住数组从 0 开始,int[] totalRow = new int[n]; 从索引 0 开始是 n 长。n 是用户输入的内容,但我们的二维数组是 n+1 高,所以我们有一个边框。我们的 totalRow 数组大小不同,因为我们不包含第一行,因为它是边框。这意味着 mazeValue 中的行 a 是 totalRow 中的行 a-1。为了从 mazeValue 到 totalRow 的交叉引用,我们减去 1。事实上,第二个数组是不需要的。我们只需要跟踪当前计数最高的行。为此,我们可以将 Horizo​​ntalPath 更改为:

public static void horizontalPath(char mazeValue[][], int n) {
    int largestRow = 0;
    int largestCount = 0;

    // int horizontalPath=0;
    int count = 0;
    for (int i = 1; i < mazeValue.length; i++) {
        for (int j = 1; j < mazeValue[i].length; j++) {
            if (mazeValue[i][j] == 'O') {
                count++;
            }
        }
        if (largestCount < count) {
            largestCount = count;
            largestRow = i - 1;
        }
        count = 0;
    }

    System.out.println("Longest horizontal path row " + largestRow + " length " + largestCount);
}

请注意,我修复了您在内部 for 循环中遇到的错误。

【讨论】:

  • 那不会影响我的柜台吗?至少我不明白它会如何
  • 这和我的horizo​​ntalPath方法无关
  • 您说得对,但是应用程序正在停止,等待输入,您和其他任何人都不知道它正在这样做。一次一个问题,这就是帮助您的障碍。
  • (我会发表我的评论作为评论,但 Stack Overflow 不允许我评论...)
  • 帮助我的拦截器?你什么意思。
猜你喜欢
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 2018-07-05
  • 1970-01-01
  • 2016-03-02
相关资源
最近更新 更多