我的程序在这里应该计算数组中最长的行以及最长的行在哪一行。试图找到我的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);
}
最佳答案
在您的主循环中,在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从1开始,而j则从1开始。
现在,请记住数组从0开始,
int[] totalRow = new int[n];
从索引0开始是n个长。n是用户输入的内容,但是我们的二维数组的高度为n + 1,所以我们有一个边界。我们的totalRow数组的大小不同,因为因为它是边框,所以我们不包括第一行。这意味着mazeValue中的a行是totalRow中的a-1行。为了从mazeValue到totalRow进行交叉引用,我们减去1。尽管如此,实际上,不需要第二个数组。我们所需要做的就是跟踪当前计数最高的行。为此,我们可以将horizontalPath更改为此: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循环中的错误。