我还没有完成它,在命名的getRows()方法中的问题,我只是尝试使用int [] []计数来计算每行中char [] []数组中的X,并将其打印出来。我做错了什么,没有工作。
package labassignment6;
import java.util.Random;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize" + size);
printArray(array);
doRows(array);
// 1.method to doRows which call
// -method with loops to count X and Os in each row
// -methos print each row's counts
//doColumns(array);
// 2.method to doColumns which call
// -method with loops to count per column
// -method to print each column's counts
//doDiagonals(array);
// 3.method to doDiagonals which calls
// -method with single loop to count top left to bottom right and
// another sigle loop to count top right to bottom left
// -method to print diagonal's counts
}//end outer array size loop
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
//code inside loops to choose X, O or space with weighting
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[][];
//counts = new int [0][2];
counts = new int[array.length - 1][array.length - 1];
getRows(array, counts);
// printRows(array, counts);
}
public static void getRows(char array[][], int counts[][]) {
// int x = 0, o = 0, s = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (i<array[j].length && array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][1] + " X");
}
}
答案 0 :(得分:0)
我已经解决了你的ArrayIndexOutOfBounds异常 - 这是由:
引起的counts = new int[array.length - 1][array.length - 1];
计数数组大小设置得太小。它需要与原始数组的长度相同,否则最终会变得太小。
但是,正如ajb所提到的,count [] []实际上应该是一维数组。如果你查看你的代码,你可以看到你只使用count [i] [0] - 所以基本上,你使用它几乎就像它是一维数组 - 因为你只使用一个索引i的每个值。所以你可以使用count [i]而不是count [i] [0],它会做同样的事情。二维方法当然可以工作,并且可以用来教你使用二维数组的有趣方法。但总的来说,更简单的方法往往是更好的方法。吻。 :)
你的问题主要存在于getRows函数中。你在内循环的每次迭代中递增counts [i] [0]的值,但是当输出每行中X的数量时,你使用值counts [i] [1] - 它永远不会被设置为任何东西(因为你只使用count [i] [0],不计算[i] [1])并且默认为0。顺便说一句,在内部循环的if语句中,您不需要检查是否i&lt;长度。外部for循环已经检查了那个条件,并且我没有在循环内的任何地方递增,它只在每个循环执行后递增1 - 然后测试条件检查它是否再次小于array.length。因此,在if语句中检查它是多余的 - 它将始终返回true,除非你在循环体内的某处递增。
以下是getRows的工作版本:
public static void getRows(char array[][], int counts[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][0] + " X");
}
}
但是,正如您所看到的,您确实应该使用一维数组 - 即使此程序可以正常工作。我将很快使用一维数组重新发布整个程序。应该只需要一分钟。
答案 1 :(得分:0)
我想我只是给他整个程序的工作代码。我希望这回答了这个问题:
package labassignment6;
import java.util.*;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize: " + size);
printArray(array);
doRows(array);
}
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[];
counts = new int[array.length];
getRows(array, counts);
}
public static void getRows(char array[][], int counts[]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i] + " X");
}
}
}