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

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

我如何连接我的String和行中的int:print('Computer is moving to ' + (i + 1));print("Computer is moving to " + (i + 1));
我无法弄清楚,因为错误一直在说“参数类型'int'不能分配给参数类型'String'”

  1. void getComputerMove() {
  2. int move;
  3. // First see if there's a move O can make to win
  4. for (int i = 0; i < boardSize; i++) {
  5. if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
  6. String curr = _mBoard[i];
  7. _mBoard[i] = computerPlayer;
  8. if (checkWinner() == 3) {
  9. print('Computer is moving to ' + (i + 1));
  10. return;
  11. } else
  12. _mBoard[i] = curr;
  13. }
  14. }
  15. // See if there's a move O can make to block X from winning
  16. for (int i = 0; i < boardSize; i++) {
  17. if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
  18. String curr = _mBoard[i]; // Save the current number
  19. _mBoard[i] = humanPlayer;
  20. if (checkWinner() == 2) {
  21. _mBoard[i] = computerPlayer;
  22. print("Computer is moving to " + (i + 1));
  23. return;
  24. } else
  25. _mBoard[i] = curr;
  26. }
  27. }
  28. }
0md85ypi

0md85ypi1#

使用字符串插值:

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

或者直接调用toString():

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

xwbd5t1u2#

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

  1. void main(){
  2. String str1 = 'Welcome to Matrix number ';
  3. int n = 24;
  4. //concatenate str1 and n
  5. String result = str1 + n.toString();
  6. print(result);
  7. }

你的情况是这样的:

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

bzzcjhmw3#

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

相关问题