英文:
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 '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;
}
}
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 'dart:math';
class VeritabaniIslemleri {
String _kullaniciAdi = "arda";
String _sifre = "123456";
bool connect() {
print("internet connection:");
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论