我试图在命令提示符板上创建可打印的内容,以便设法在CMD中创建一个TicTacToe游戏。虽然,当我为我的电路板和单元格创建类时,Java在我的print和println下抛出了一个错误,告诉我:
symbol: method println() -or- method print() .etc...
location: class board
error: cannot find symbol
我的代码有什么问题?这是我的整个.java文件:
我只想要它编译,而不是运行
import acm.program.*;
public class board {
private static final int ROWS=3;
private static final int COLS=3;
private int[][] board1 = new int[ROWS][COLS];
//constructor
public board() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board1[i][j]=0;
printBoard();
}
}
}
public void printBoard() {
for(int row =0; row<ROWS; row++) {
for (int col=0; col<COLS; col++) {
printCell(board1[row][col]);
if (col != (COLS-1)) {
print("|"); // print vertical partition
}
}
println();
if (row !=(ROWS-1)) {
println("-----------");
}
}
println();
}
public void printCell(int content) {
if (content == 0) {print(" ");}
}
}
它仅通过用system.out替换print()和println()进行编译。但这太奇怪了。ACM软件包包括诸如println()和print()之类的方法,以使其变得更容易。但现在它是固定的。谢谢你。
编辑2:为了与print()和println()进行编译,需要具有:“公共类板扩展程序”,而不仅仅是“公共类板”
这是更正的代码:
public class board {
private static final int ROWS=3;
private static final int COLS=3;
private int[][] board1 = new int[ROWS][COLS];
//constructor
public board() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board1[i][j]=0;
printBoard();
}
}
}
public void printBoard(){
for(int row =0; row<ROWS; row++){
for (int col=0; col<COLS; col++){
printCell(board1[row][col]);
if (col != (COLS-1)) {
System.out.print("|"); // print vertical partition
}
}
System.out.println("");
if (row !=(ROWS-1)) {
System.out.println("-----------");
}
}
System.out.println();
}
public void printCell(int content) {
if (content == 0) {System.out.print(" ");}
}
}
您只是错过了一些针对打印语句的“ System.out”调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句