【问题标题】:How can I define a boolean value under an if statement如何在 if 语句下定义布尔值
【发布时间】:2022-01-08 07:37:16
【问题描述】:
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:错误:找不到吸气剂:'isStudent'。别的 if(!isSmart && !isStudent) {

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

【问题讨论】:

    标签: dart if-statement boolean


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢。它有效,但我仍在学习飞镖,不知道 `ate,final 等,但感谢您的回答
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-03-01
        • 2013-03-26
        • 2018-04-17
        • 2014-08-19
        • 2015-12-21
        • 2021-08-03
        • 1970-01-01
        • 2013-03-03
        相关资源
        最近更新 更多