我的程序应该计算数组中最长的行以及最长的行。我的计数停滞不前,我的眼睛因试图找到我的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);
}
&#13;
答案 0 :(得分:1)
在您的主要内容System.out.println("ENTER A PATH: ");
之后,在您的for循环中,它等待用户输入,但不会再次询问该路径。将System.out.println("ENTER A PATH: ");
移动到该巢中的其他地方。
以下是我的代码:
/**
* @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循环中的错误。