dart 中使用 stdin 输出数字的问题

发布于 2025-01-09 04:45:11 字数 529 浏览 1 评论 0原文

带有数字的输出不起作用,我只能写第一个数字,之后应用程序将关闭。


void main(List<String> args) {
  print("Escreva o numero de carros vendidos pelo funcionario ");
  int nuyCar = stdin.readByteSync().toInt();

  print("Escreva o salario fixo do funcionario: ");
  double fixSalary = stdin.readByteSync().toDouble();

  print("Escreva o valor arrecadado das vendas do funcionario: ");
  double priceCollected = stdin.readByteSync().toDouble();

  print(nuyCar);
  print(fixSalary);
  print(priceCollected);
}

The output with numbers does not work, I can only write the first number and after that the application closes.


void main(List<String> args) {
  print("Escreva o numero de carros vendidos pelo funcionario ");
  int nuyCar = stdin.readByteSync().toInt();

  print("Escreva o salario fixo do funcionario: ");
  double fixSalary = stdin.readByteSync().toDouble();

  print("Escreva o valor arrecadado das vendas do funcionario: ");
  double priceCollected = stdin.readByteSync().toDouble();

  print(nuyCar);
  print(fixSalary);
  print(priceCollected);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2025-01-16 04:45:11

嗯,Dart 是严重依赖空检查的语言之一。在这里阅读更多相关信息 -> https://dart.dev/null-safety

您应该养成检查这些的习惯。此外,您还可以使用 readLineSync(),而不是 readBytes()

你的程序可以这样写:

import 'dart:io';

void main(List<String> args) {

  int? nuyCar = 0;
  double? fixSalary = 0.0;
  double? priceCollected = 0.0;

  print("Escreva o numero de carros vendidos pelo funcionario ");
  var line = stdin.readLineSync();
  if (line != null) {
    nuyCar = int.parse(line);
  }

  print("Escreva o salario fixo do funcionario: ");
  line = stdin.readLineSync();
  if (line != null) {
    fixSalary = double.parse(line);
  }

  print("Escreva o valor arrecadado das vendas do funcionario: ");
  line = stdin.readLineSync();
  if (line != null) {
    priceCollected = double.parse(line);
  }

  print(nuyCar);
  print(fixSalary);
  print(priceCollected);
}

Well, Dart is one of those languages that heavily relies on null-checks. Read more about it here -> https://dart.dev/null-safety

You should get in habit of checking for those. Also, instead of readBytes(), you can use readLineSync().

Your program could be written as such:

import 'dart:io';

void main(List<String> args) {

  int? nuyCar = 0;
  double? fixSalary = 0.0;
  double? priceCollected = 0.0;

  print("Escreva o numero de carros vendidos pelo funcionario ");
  var line = stdin.readLineSync();
  if (line != null) {
    nuyCar = int.parse(line);
  }

  print("Escreva o salario fixo do funcionario: ");
  line = stdin.readLineSync();
  if (line != null) {
    fixSalary = double.parse(line);
  }

  print("Escreva o valor arrecadado das vendas do funcionario: ");
  line = stdin.readLineSync();
  if (line != null) {
    priceCollected = double.parse(line);
  }

  print(nuyCar);
  print(fixSalary);
  print(priceCollected);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文