Random().nextBool() 的工作方式很奇怪。我该如何解决?

huangapple go评论109阅读模式
英文:

Random().nextBool() works in a silly way. How do I solve it?

问题

import '6veritabani_islemleri.dart';

main(List<String> args) {
  VeritabaniIslemleri db = VeritabaniIslemleri();

  bool sonuc = db.connect();

  if (sonuc == true) {
    print("Connected");
  } else {
    print("Not connected.");
  }
}
import 'dart:math';

class VeritabaniIslemleri {
  String _kullaniciAdi = "arda";
  String _sifre = "123456";

  bool connect() {
    print("internet connection:");
    print(isThereInternet());
    if (isThereInternet() == true) {
      if (_kullaniciAdi == "arda" && _sifre == "123456") {
        return true;
      } else
        return false;
    } else
      return false;
  }

  bool isThereInternet() {
    bool deger = Random().nextBool();
    if (deger) {
      return true;
    } else
      return false;
  }
}

在第二部分中,如果isThereInternet()函数返回false,connect()函数不应该自动返回false吗?但是在我的代码中,即使isThereInternet()返回false,connect()也可以返回true。

当我将Random().nextBool()更改为false或true布尔值时,代码正常工作。为什么?提前感谢您。

英文:
import &#39;6veritabani_islemleri.dart&#39;;

main(List&lt;String&gt; args) {
  VeritabaniIslemleri db = VeritabaniIslemleri();

  bool sonuc = db.connect();

  if (sonuc == true) {
    print(&quot;Connected&quot;);
  } else {
    print(&quot;Not connected.&quot;);
  }
}


import &#39;dart:math&#39;;

class VeritabaniIslemleri {
  String _kullaniciAdi = &quot;arda&quot;;
  String _sifre = &quot;123456&quot;;

  bool connect() {
    print(&quot;internet connection:&quot;);
    print(isThereInternet());
    if (isThereInternet() == true) {
      if (_kullaniciAdi == &quot;arda&quot; &amp;&amp; _sifre == &quot;123456&quot;) {
        return true;
      } else
        return false;
    } else
      return false;
  }

  bool isThereInternet() {
    bool deger = Random().nextBool();
    if (deger) {
      return true;
    } else
      return false;
  }
}

In the second part, if isThereInternet() function returns false, shouldn't connect() function automatically returns false? But in my code even if isThereInternet() returns false, connect() can returns true.

When I change Random().nextBool() to false or true bool value, the code works fine. Why? Thanks in advance.

答案1

得分: 2

你的代码调用了 isThereInternet() 两次,因此会生成两个结果。这两个结果可能会不同。我稍微修改了你的代码:

import 'dart:math';

class VeritabaniIslemleri {
  String _kullaniciAdi = "arda";
  String _sifre = "123456";

  bool connect() {
    print("网络连接:");
    final hasInternet = isThereInternet();
    print(hasInternet);
    if (hasInternet) {
      if (_kullaniciAdi == "arda" && _sifre == "123456") {
        return true;
      } else
        return false;
    } else
      return false;
  }

  bool isThereInternet() {
    return Random().nextBool();
  }
}
英文:

Your code call isThereInternet() twice, so it generates two result, And the two results may be different.
I have modified you code a little:

import &#39;dart:math&#39;;

class VeritabaniIslemleri {
  String _kullaniciAdi = &quot;arda&quot;;
  String _sifre = &quot;123456&quot;;

  bool connect() {
    print(&quot;internet connection:&quot;);
    final hasInternet = isThereInternet();
    print(hasInternet);
    if (hasInternet) {
      if (_kullaniciAdi == &quot;arda&quot; &amp;&amp; _sifre == &quot;123456&quot;) {
        return true;
      } else
        return false;
    } else
      return false;
  }

  bool isThereInternet() {
    return Random().nextBool();
  }
}

huangapple
  • 本文由 发表于 2023年7月20日 11:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定