我不知道为什么输出是用ascii代码而不是整数值打印的,我的代码有什么问题?

import 'dart:io';

void main() {
  int n = 0;
  print("input: ");

  do {
    n = stdin.readByteSync();
  } while (n>1000 || n<0);

  if (n % 4 == 0) {
    print("output: ");
    print(n);
    n++;
  } else {
    print("output: ");
    n--;
    print(n);
  }
}

console - 为什么输出以ASCII码而不是整数打印?-LMLPHP

最佳答案

使用 String 将该值获取为stdin.readLineSync(),然后将 parse 转换为int,如下所示:

import 'dart:io';

void main() {
  int n = 0;
  print("input: ");

  do {
    n = int.parse(stdin.readLineSync());
  } while (n>1000 || n<0);

  if (n % 4 == 0) {
    print("output: ");
    print(n);
    n++;
  } else {
    print("output: ");
    n--;
    print(n);
  }
}

10-01 21:06
查看更多