1

我试图在命令提示板上创建可打印的内容,以便设法在 CMD 中创建井字游戏。虽然,当我为我的板子和单元格创建类时,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("   ");}
    }

}

它只需将 print() 和 println() 替换为 system.out 即可编译。但这太奇怪了。ACM 包包括 println() 和 print() 等方法,以使其更容易。但现在它是固定的。谢谢你。

编辑 2:为了使用 print() 和 println() 进行编译,需要:“公共类板扩展程序”而不仅仅是“公共类板”

4

3 回答 3

3

尝试替换println()print()

System.out.print();
System.out.println();

如果要使用 ACM,则必须acm.jar在类路径中有文件,并且必须Program在类中扩展类,board例如:class board extends Program{}

另见

于 2014-02-07T03:30:58.057 回答
1

这是更正后的代码:

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”的调用。

于 2014-02-07T03:33:17.913 回答
0

Change println() by

System.out.print(); or   
System.out.println();

For More

于 2014-02-07T03:35:47.360 回答