检查键和值
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 27 课,共 65 课。
在访问 Map 中的数据之前,务必先确认你要查找的键或值确实存在。Dart 提供了两个方法来完成这项检查:containsKey() 和 containsValue()。
containsKey() 方法会检查映射中是否存在指定的键。如果找到该键,则返回 true,否则返回 false。在尝试访问值之前进行检查特别有用,可以帮助你避免潜在的错误。
Map<String, String> userPermissions = {
'user123': 'admin',
'user456': 'editor',
'user789': 'viewer'
};
bool hasUser = userPermissions.containsKey('user123');
print(hasUser); // true
bool hasUnknownUser = userPermissions.containsKey('user999');
print(hasUnknownUser); // false类似地,containsValue() 会检查映射中的任何位置是否存在特定值。此方法会搜索所有值,如果找到匹配项,就返回 true。
bool hasAdmin = userPermissions.containsValue('admin');
print(hasAdmin); // true这些方法对于编写安全的代码至关重要,可以优雅地处理缺失的数据,避免在处理动态映射内容时发生运行时错误。
挑战
简单创建一个管理用户身份验证系统的程序,通过验证用户凭据并检查权限级别来进行管理。你的程序应该:
- 读取一个表示系统名称的字符串输入
- 读取多组输入,分别表示用户名及其权限级别(当你收到
"users_done"时,输入结束) - 读取一个表示待检查用户名的字符串输入
- 读取一个表示待验证权限级别的字符串输入
- 创建一个 Map,将用户名作为键,将其权限级别作为值
- 使用
containsKey()检查系统中是否存在该用户名 - 使用
containsValue()检查系统中的任何位置是否存在指定权限级别 - 按照下方显示的确切格式输出身份验证分析结果
例如,如果系统名称是 "Corporate Network",用户包括权限为 "admin" 的 "alice"、权限为 "editor" 的 "bob"、权限为 "viewer" 的 "charlie"、权限为 "admin" 的 "diana",待检查的用户名是 "alice",待验证的权限级别是 "admin",那么你的程序应该输出:
System: Corporate Network
User Database: {alice: admin, bob: editor, charlie: viewer, diana: admin}
Authentication Check:
Username 'alice' exists: true
Permission 'admin' exists in system: true
User 'alice' has permission 'admin': true
Access Status: GRANTED - User authenticated with valid permissions
Security Level: High (admin access detected)如果系统名称是 "School Portal",用户包括权限为 "instructor" 的 "teacher1"、权限为 "student" 的 "student1"、权限为 "administrator" 的 "admin1",待检查的用户名是 "guest",待验证的权限级别是 "student",那么你的程序应该输出:
System: School Portal
User Database: {teacher1: instructor, student1: student, admin1: administrator}
Authentication Check:
Username 'guest' exists: false
Permission 'student' exists in system: true
User 'guest' has permission 'student': false
Access Status: DENIED - Username not found in system
Security Level: Low (user not registered)如果系统名称是 "Library System",用户包括权限为 "staff" 的 "librarian"、权限为 "member" 的 "member1"、权限为 "member" 的 "member2",待检查的用户名是 "librarian",待验证的权限级别是 "premium",那么你的程序应该输出:
System: Library System
User Database: {librarian: staff, member1: member, member2: member}
Authentication Check:
Username 'librarian' exists: true
Permission 'premium' exists in system: false
User 'librarian' has permission 'premium': false
Access Status: DENIED - Permission level not available
Security Level: Medium (valid user, invalid permission)你的程序必须使用 containsKey() 验证用户名是否存在,并使用 containsValue() 检查系统中的任何位置是否存在该权限级别。要检查特定用户是否拥有特定权限,请将用户的实际权限级别与请求的权限级别进行比较。根据结果确定安全级别:如果授予访问权限且用户拥有 admin 类型的权限(admin、administrator、staff),则为 "High";如果用户存在但权限不正确,则为 "Medium";如果用户不存在,则为 "Low"。输入格式为:系统名称,然后是交替出现的用户名和权限级别,以 "users_done" 结束,之后是待检查的用户名和待验证的权限级别。
亲手试试
import 'dart:io';
void main() {
// 读取系统名称
String? systemName = stdin.readLineSync();
// 创建一个 Map,存储用户名及其权限级别
Map<String, String> userDatabase = {};
// 读取用户数据,直到遇到 "users_done"
while (true) {
String? input = stdin.readLineSync();
if (input == "users_done") {
break;
}
String username = input!;
String? permission = stdin.readLineSync();
userDatabase[username] = permission!;
}
// 读取要检查的用户名和要验证的权限级别
String? usernameToCheck = stdin.readLineSync();
String? permissionToVerify = stdin.readLineSync();
// TODO:在下方编写你的代码
// 用 containsKey() 检查用户名是否存在
// 用 containsValue() 检查系统中是否存在该权限级别
// 检查指定用户是否拥有指定权限
// 确定访问状态和安全级别:
// 当访问获准,且权限为 admin、administrator, 或 staff 时,使用 "High (admin access detected)"
// 当访问获准,但权限不是管理员类型时,使用 "Medium (standard access)"
// 当用户存在,但权限不匹配时,使用 "Medium (valid user, invalid permission)"
// 如果用户不存在,输出 "Low (user not registered)"
// 打印身份验证分析结果
print("System: $systemName");
print("User Database: $userDatabase");
print("Authentication Check:");
// 在这里添加你的输出语句
}这节课包含一个小测验。开始学习即可作答,并记录你的进度。
逻辑与流程控制中的所有课
自己练习: Dart 在线编译器