Dart -如何连接字符串和整数

rslzwgfq  于 2023-10-13  发布在  其他
关注(0)|答案(3)|浏览(96)

我如何连接我的String和行中的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;
      }
    }
  }
0md85ypi

0md85ypi1#

使用字符串插值:

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

或者直接调用toString():

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

xwbd5t1u2#

你可以简单地使用.toString将整数转换为String:

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());
bzzcjhmw

bzzcjhmw3#

var intValue = Random().nextInt(5)+1;   // 1 <--> 5
 var string = "the nb is $intValue random nb ";

相关问题