如何在 if 语句下定义布尔值

How can I define a boolean value under an if statement

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: Error: Getter not found: 'isStudent'. else if(!isSmart && !isStudent) {

这里有什么问题?我该如何解决?

代码有一些冗余和图片可能导致手头的错误,避免定义您在代码和代码中用于比较的某些变量,按如下方式重写应该可以解决它,但我无法测试自己,因为我你的 headers 没有共享。

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");
  };

}

isStudentisSmart 变量在您创建的 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");
  }
}

我认为问题在于您的 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;
  }
  ...