繁体   English   中英

Dart - 如何连接字符串和我的 integer

[英]Dart - How to concatenate a String and my integer

如何连接我的字符串和行中的 int: print('Computer is moving to ' + (i + 1)); print("Computer is moving to " + (i + 1));

我无法弄清楚,因为错误一直说“参数类型'int'不能分配给参数类型'String'

void getComputerMove() {
    int move;

    // First see if there's a move O can make to win
    for (int i = 0; i < boardSize; i++) {
      if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
        String curr = _mBoard[i];
        _mBoard[i] = computerPlayer;
        if (checkWinner() == 3) {
          print('Computer is moving to ' + (i + 1));
          return;
        } else
          _mBoard[i] = curr;
      }
    }

    // See if there's a move O can make to block X from winning
    for (int i = 0; i < boardSize; i++) {
      if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
        String curr = _mBoard[i]; // Save the current number
        _mBoard[i] = humanPlayer;
        if (checkWinner() == 2) {
          _mBoard[i] = computerPlayer;
          print("Computer is moving to " + (i + 1));
          return;
        } else
          _mBoard[i] = curr;
      }
    }
  }

使用字符串插值:

print("Computer is moving to ${i + 1}"); 

或者只是调用 toString():

print("Computer is moving to " + (i + 1).toString()); 

您可以简单地使用.toString将您的 integer 转换为字符串:

void main(){
     
    String str1 = 'Welcome to Matrix number ';
    int n = 24;
     
    //concatenate str1 and n
    String result = str1 + n.toString();
     
    print(result);
}

在你的情况下,它会是这样的:

print("Computer is moving to " + (i + 1).toString()); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM