import "dart:io";
import "dart:math";
String promptStudent(){
print("are you a student?");
String Student = stdin.readLineSync()!;
return Student;
}
String promptSmart(){
print("2 2=?");
String Smart = stdin.readLineSync()!;
return Smart;
}
void main() {
String student = promptStudent();
if(student == "yes"){
bool isStudent = true;
} else() {
bool isStudent = false;
};
String smart = promptSmart();
if(smart == "4"){
bool isSmart = true;
} else() {
bool isSmart = false;
};
if(isSmart && isStudent) {
print("he is a smart student");
}
else if(isSmart && !isStudent) {
print("he is smart but not a student");
}
else if(!isSmart && isStudent) {
print("he isnt smart but a student");
}
else if(!isSmart && !isStudent) {
print("he is not smart and not a student");
};
}
我想通過用戶輸入設定一個布林值(如果 2 2 的答案是 4 他很聰明等),但我遇到了如下問題:
main.dart:42:24: 錯誤:找不到 Getter:'isStudent'。否則 if(!isSmart && !isStudent) {
這里有什么問題?我該如何解決?
uj5u.com熱心網友回復:
在isStudent和isSmart變數具有塊范圍在您創建的if-else陳述句。它們的范圍僅限于{}包圍它們的大括號。因此,您無法在這些部分之外訪問它們。
要訪問 if-else 塊范圍之外的變數,您需要在它之外宣告變數。在 if-else 塊之外宣告變數并在 if-else 塊內分配它們。下面的代碼還修復了各種其他語法錯誤,包括洗掉else陳述句后面的大括號、if-else 塊后面的分號、洗掉未使用的匯入和修復命名約定。
import "dart:io";
String promptStudent() {
print("are you a student?");
String student = stdin.readLineSync()!;
return student;
}
String promptSmart() {
print("2 2=?");
String smart = stdin.readLineSync()!;
return smart;
}
void main() {
String student = promptStudent();
late final bool isStudent;
if (student == "yes") {
isStudent = true;
} else {
isStudent = false;
}
String smart = promptSmart();
late final bool isSmart;
if (smart == "4") {
isSmart = true;
} else {
isSmart = false;
}
if (isSmart && isStudent) {
print("he is a smart student");
} else if (isSmart && !isStudent) {
print("he is smart but not a student");
} else if (!isSmart && isStudent) {
print("he isnt smart but a student");
} else if (!isSmart && !isStudent) {
print("he is not smart and not a student");
}
}
您還可以通過多種方式簡化代碼,尤其是在分配布林值時。
這個
late final bool isStudent;
if (student == "yes") {
isStudent = true;
} else {
isStudent = false;
}
可以簡化為這個單行:
final bool isStudent = student == "yes";
這使您的代碼變得更簡單:
import "dart:io";
String promptStudent() {
print("are you a student?");
return stdin.readLineSync()!;
}
String promptSmart() {
print("2 2=?");
return stdin.readLineSync()!;
}
void main() {
String student = promptStudent();
final bool isStudent = student == "yes";
String smart = promptSmart();
final bool isSmart = smart == "4";
if (isSmart && isStudent) {
print("he is a smart student");
} else if (isSmart && !isStudent) {
print("he is smart but not a student");
} else if (!isSmart && isStudent) {
print("he isnt smart but a student");
} else if (!isSmart && !isStudent) {
print("he is not smart and not a student");
}
}
uj5u.com熱心網友回復:
我相信問題在于您的 isStudent 和 isSmart 的范圍。嘗試這個:
void main() {
String student = promptStudent();
bool isStudent = false;
if(student == "yes"){
isStudent = true;
}
String smart = promptSmart();
bool isSmart = false;
if(smart == "4"){
isSmart = true;
}
...
uj5u.com熱心網友回復:
代碼有一些多余的圖片,可能會導致手頭的錯誤,避免定義一些用于比較的變數的定義,如下重寫應該可以解決它,但我無法測驗自己,因為我的標題沒有共享。
import "dart:io";
import "dart:math";
String promptStudent(){
print("are you a student?");
String Student = stdin.readLineSync()!;
return Student;
}
String promptSmart(){
print("2 2=?");
String Smart = stdin.readLineSync()!;
return Smart;
}
void main() {
bool isStudent = false;
bool isSmart = true;
String student = promptStudent();
if(student == "yes"){
isStudent = true;
}
String smart = promptSmart();
if(smart == "4"){
isSmart = true;
}
if(isSmart && isStudent) {
print("he is a smart student");
}
else if(isSmart && !isStudent) {
print("he is smart but not a student");
}
else if(!isSmart && isStudent) {
print("he isnt smart but a student");
}
else if(!isSmart && !isStudent) {
print("he is not smart and not a student");
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371524.html
上一篇:如果出現任何問題,有什么方法可以防止在chrome控制臺上拋出錯誤?
下一篇:輪盤游戲連勝計數器
