You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于用户输入计算申请者总分?(含代码问题排查)

代码问题分析与修正

现有代码的核心问题

  • 赋值语法错误:spellsPoints: 10 * spellsCasted; 这里误用了冒号(C++里是标签语法),根本没给spellsPoints赋值,变量会保留内存里的随机垃圾值,直接导致最终结果完全错误,必须把冒号改成赋值符号=。
  • 冗余初始化:float total_points = 1; 这个初始值毫无意义,后续会被重新赋值,直接声明变量即可。

修正后的代码

/*-------------------------------------输入部分-----------------------*/

// 输入申请者成功施放的法术数量
cout << "请输入申请者成功施放的法术数量:" << endl;
int spellsCasted;
cin >> spellsCasted;

// 输入申请者拥有的魔杖数量
cout << "请输入申请者拥有的魔杖数量:" << endl;
int wandsOwned;
cin >> wandsOwned;

/*----------------------------------------计算部分---------------------------*/

// 计算申请者基础得分
float total_points;
int spellsPoints, wandsPoints;

spellsPoints = 10 * spellsCasted; // 修正赋值语法
wandsPoints = 2 * wandsOwned;
total_points = spellsPoints + wandsPoints;

// 若后续补充捐赠百分比计算,可在此添加示例代码:
// float donation_percentage;
// cout << "请输入捐赠百分比(如输入50代表50%):" << endl;
// cin >> donation_percentage;
// total_points = total_points + (total_points * donation_percentage / 100);

cout << "申请者的总得分是:" << total_points << endl;

补充说明

如果后续需要加入捐赠百分比的计算,要明确输入的百分比形式:如果输入的是0-100的整数(比如50代表50%),就用注释里的代码;如果输入的是0-1的小数(比如0.5代表50%),直接去掉除以100的部分即可。

内容的提问来源于stack exchange,提问作者jieya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:00:46