Logo
Section 1 / 3

使用 AI 做出决策

通过构建一个智能恒温器来学习 C++ 的 if/else 语句和比较运算符,该恒温器会决定是否需要加热或冷却你的家。

导入

使用 AI 做出决策

通过构建一个智能恒温器来学习 C++ 的 if/else 语句和比较运算符,该恒温器会决定是否需要加热或冷却你的家。

...
1 int age; 2 cout << "How old are you? "; 3 cin >> age; 4 5 if (age >= 18) { 6 cout << "You can vote!" << endl; 7 } 8 else { 9 cout << "Sorry, too young." << endl; 10 }
1#include <iostream> 2using namespace std; 3 4int main() { 5 double current_temperature = 15; 6 7 if (current_temperature < 18) { 8 cout << "Switch to Fan mode" << endl; 9 } else { 10 cout << "Keep cooling" << endl; 11 } 12 13 return 0; 14}
1#include <iostream> 2using namespace std; 3 4int main() { 5 double current_temperature = 15; 6 double preferred_temperature = 18; 7 8 if (current_temperature < preferred_temperature) { 9 cout << "Switch to Fan mode" << endl; 10 } else { 11 cout << "Keep cooling" << endl; 12 } 13 14 return 0; 15}

代码示例

1cin.fail(); // 检查上次输入是否失败(例如,用户输入了文本而非数字) 2cin.clear(); // 清除错误标志,允许新的输入 3cin.ignore(); // 清除缓冲区中残留的无效输入

No configuration found for ID: beb49aef1123b0694ab0dc6fb3e64e4291a6b84b7fe4a65dd053de217adf764d

智能家居应用用户界面

1请输入您理想的舒适温度: 22 2理想舒适温度: 22 3当前温度: 25 4 5操作: 打开空调 6 7提示: 建议在非高峰时段运行以节省能源。
PageFlux AI 助手

👋 你好!我是 PageFlux AI 助手,专门帮助您理解课程内容。有什么问题尽管问我吧!

💡 选中文本后点击"AI 询问",或 Ctrl+Enter 发送消息

No configuration found for ID: b1b9a383e10659726cb82ee9ec72e8c554b1425176b3ffcb947398d617b11e93

如果运行代码时遇到任何错误,请直接复制终端中的错误信息并粘贴到 AI 提示框中。您可以告诉 AI 类似的内容:

“您提供的代码产生了以下错误:[粘贴错误信息]”

这将帮助 AI 更有效地协助您解决问题。

1#include <iostream> 2using namespace std; 3 4int main() { 5 double preferred_temperature; 6 double current_temperature; 7 bool is_peak_hour = true; // 将其改为 false 以表示非高峰时段 8 9 // --- 输入部分 --- 10 cout << "请输入您理想的舒适温度: "; 11 cin >> preferred_temperature; 12 13 // 输入验证检查 14 if (cin.fail()) { 15 cout << "输入无效!请为温度输入数值。" << endl; 16 cin.clear(); // 重置错误标志 17 cin.ignore(1000, '\n'); // 清除无效输入 18 return 0; // 提前退出以避免后续错误行为 19 } 20 21 cout << "理想舒适温度: " << preferred_temperature << endl; 22 23 cout << "请输入当前温度: "; 24 cin >> current_temperature; 25 26 // 输入验证检查 27 if (cin.fail()) { 28 cout << "输入无效!请为温度输入数值。" << endl; 29 cin.clear(); 30 cin.ignore(1000, '\n'); 31 return 0; 32 } 33 34 cout << "当前温度: " << current_temperature << endl << endl; 35 36 // --- 决策逻辑 --- 37 string action; 38 39 if (current_temperature < preferred_temperature) { 40 action = "打开加热器"; 41 } 42 else if (current_temperature > preferred_temperature) { 43 action = "打开空调"; 44 } 45 else { 46 action = "无需操作"; 47 } 48 49 cout << "操作: " << action << endl; 50 51 // --- 节能额外提示 --- 52 if (is_peak_hour && (action == "打开加热器" || action == "打开空调")) { 53 cout << "\n提示: 建议在非高峰时段运行以节省能源。" << endl; 54 } 55 56 return 0; 57}
1#include <iostream> 2using namespace std; 3 4int main() { 5 double preferred_temperature; 6 double current_temperature; 7 bool is_peak_hour = true; // 将其改为 false 以表示非高峰时段 8 9 // --- 输入部分 --- 10 cout << "请输入您理想的舒适温度: "; 11 cin >> preferred_temperature; 12 13 // 输入验证检查 14 if (cin.fail()) { 15 cout << "输入无效!请为温度输入数值。" << endl; 16 cin.clear(); // 重置错误标志 17 cin.ignore(1000, '\n'); // 清除无效输入 18 return 0; // 提前退出以避免后续错误行为 19 } 20 21 cout << "理想舒适温度: " << preferred_temperature << endl; 22 23 cout << "请输入当前温度: "; 24 cin >> current_temperature; 25 26 // 输入验证检查 27 if (cin.fail()) { 28 cout << "输入无效!请为温度输入数值。" << endl; 29 cin.clear(); 30 cin.ignore(1000, '\n'); 31 return 0; 32 } 33 34 cout << "当前温度: " << current_temperature << endl << endl; 35 36 // --- 决策逻辑 --- 37 string action; 38 39 if (current_temperature < preferred_temperature) { 40 action = "打开加热器"; 41 } 42 else if (current_temperature > preferred_temperature) { 43 action = "打开空调"; 44 } 45 else { 46 action = "无需操作"; 47 } 48 49 cout << "操作: " << action << endl; 50 51 // --- 节能额外提示 --- 52 if (is_peak_hour && (action == "打开加热器" || action == "打开空调")) { 53 cout << "\n提示: 建议在非高峰时段运行以节省能源。" << endl; 54 } 55 56 return 0; 57}

代码示例

1洗衣负载大小: 7 2高峰时段: True 3建议: 使用标准洗涤模式 4提示: 建议在非高峰时段运行以节省电费。 5保持洁净,节约能源!

No configuration found for ID: 122c47ef499ee48cfc4e9695ddd54ec801aed96e61093e0ec6578e0e82d4e404

1#include <iostream> 2using namespace std; 3 4int main() { 5 double laundry_load; 6 bool is_peak_hour = true; // 将其改为 false 以表示非高峰时段 7 8 // --- 输入部分 --- 9 cout << "请输入洗衣负载重量(单位:kg): "; 10 cin >> laundry_load; 11 12 // 简单输入验证 13 if (cin.fail()) { 14 cout << "输入无效!请为洗衣负载输入一个数字。" << endl; 15 cin.clear(); // 重置错误标志 16 cin.ignore(1000, '\n'); // 清除任何残留的无效输入 17 return 0; // 提前终止程序 18 } 19 20 string advice; 21 22 // --- 决策逻辑 --- 23 if (laundry_load < 5) { 24 advice = "使用快速洗涤模式"; 25 } 26 else if (laundry_load == 5) { 27 advice = "两种模式均可"; 28 } 29 else { 30 advice = "使用标准洗涤模式"; 31 } 32 33 cout << "建议: " << advice << endl; 34 35 // --- 额外节能提示 --- 36 if (is_peak_hour && advice != "两种模式均可") { 37 cout << "提示: 建议在非高峰时段运行以节省电费。" << endl; 38 } 39 40 cout << "保持洁净,节约能源!" << endl; 41 42 return 0; 43}
1#include <iostream> 2using namespace std; 3 4int main() { 5 double laundry_load; 6 bool is_peak_hour = true; // 将其改为 false 以表示非高峰时段 7 8 // --- 输入部分 --- 9 cout << "请输入洗衣负载重量(单位:kg): "; 10 cin >> laundry_load; 11 12 // 简单输入验证 13 if (cin.fail()) { 14 cout << "输入无效!请为洗衣负载输入一个数字。" << endl; 15 cin.clear(); // 重置错误标志 16 cin.ignore(1000, '\n'); // 清除任何残留的无效输入 17 return 0; // 提前终止程序 18 } 19 20 string advice; 21 22 // --- 决策逻辑 --- 23 if (laundry_load < 5) { 24 advice = "使用快速洗涤模式"; 25 } 26 else if (laundry_load == 5) { 27 advice = "两种模式均可"; 28 } 29 else { 30 advice = "使用标准洗涤模式"; 31 } 32 33 cout << "建议: " << advice << endl; 34 35 // --- 额外节能提示 --- 36 if (is_peak_hour && advice != "两种模式均可") { 37 cout << "提示: 建议在非高峰时段运行以节省电费。" << endl; 38 } 39 40 cout << "保持洁净,节约能源!" << endl; 41 42 return 0; 43}

变量、数据类型和输入

Else-If 链和嵌套条件