【发布时间】: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 < mazeValue.length - 2; i++-> 您可能的意思是 i = 1?!
标签: java arrays for-loop counter