Java-如何解决我的 TicTacToe 游戏的方法无法应用于给定类型错误?

标签 java arrays

我正在制作一个 TicTacToe 游戏,其中包含初始化游戏、显示棋盘、显示游戏选项、说出轮到谁、检查获胜者、添加一步、重新启动游戏、检查棋盘是否已满的方法,并玩游戏。我在添加移动方法和玩游戏方法时遇到问题。

  public boolean addMove(int row, int column) {
  boolean nonacceptable = true;
  while (nonacceptable) {
     System.out.println("Which row and column would you like to enter your mark? Enter the row and column between 0 and 2 separated by a space.");
     row = input.nextInt();
     column = input.nextInt();
     if ((row >= 0 && row <=2) && (column >= 0 && column <=2)) { //make sure user entered a number between 0 and 2
        if (gameBoard[row][column] != ' ') 
           System.out.println("Sorry, this position is not open!");

        else {
           gameBoard[row][column] = currentMark;
           nonacceptable = false;
        }
     }   
     else 
        System.out.println("That position is not between 0 and 2!");
     }
     return nonacceptable;     

}

这是我的玩法:

  public void letsPlay() {
  while (true) {
     displayBoard();
     gameOptions();
     int choice = input.nextInt();
     if (choice == 1) {
        if (addMove()) {
           displayBoard();
           checkWinner();
           System.exit(0);
        }
        else if (!boardFull()) {
           displayBoard();
           System.out.println("Board full!");
           System.exit(0);
        }
        else {
           whoseTurn();
        }
     }
     else if (choice == 2) 
        restart();
     else if (choice == 3) 
        System.exit(0);
     else 
        System.out.println("Try again!");
  }
}

当我编译时,我收到此错误:TicTacToe.java:110: error: method addMove in class TicTacToe can not be apply to given types; 如果(添加移动()){ ^ 必需:整数,整数 发现:没有参数 原因:实际参数列表和正式参数列表的长度不同 1 个错误

我该如何解决这个问题?

最佳答案

很清楚了。

您的 addMove 函数签名接受两个参数

public boolean addMove(int row, int column) { 
                         ^          ^

每当您想要调用或使用 addMove 函数时,您都必须遵循您在签名函数中定义的规则。

因此,解决方案是在调用 addMove 函数的地方传递两个 int 类型的参数,这样问题就消失了。

注:Read more about how to define a function and call it in Java

关于Java-如何解决我的 TicTacToe 游戏的方法无法应用于给定类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32897367/

相关文章:

java - 带有产品 flavor 和 Kotlin 的 ClassNotFoundException

java - Apache Ignite 中的 session 写入超时问题

java - 我的 java 应用程序不读取我的文件(maven 项目)

c++ - 如何在 Turbo C++ 中创建基于字符的数组?

java - Spring mvc maven - 添加库

java - 分割googleplacepicker的纬度和经度,place.getLatLng()

arrays - Perl在全局数组中分配一些内存的方法

java - Java中有没有一种干净的方法将int数组的一部分输出为字符串?

ios - 允许数据在所有 Swift3 文件中持久存在

javascript - 将 json 从 fetch 函数映射到 setstate