c# - 我需要帮助从我的代码中检索用户输入
问题描述
我只学习了大约一周的 C#,并且我正在处理这个个人项目。我正在构建一个程序来计算销售人员的每月奖金。在我的代码末尾,我需要告诉用户总奖金金额,奖金不能超过 230 美元。
我的问题是,如何检索用户输入以获得总计以及如何设置 230 美元的限制?
任何帮助将不胜感激。
我尝试使用更多 if 语句来检索用户已经输入的内容。
Console.WriteLine("What is the total number of items sold?");
int itemsSold = Convert.ToInt16(Console.ReadLine());
int itemBonus = 50;
if (itemsSold > 20)
{
Console.WriteLine("Your items sold bonus is {0} dollars" ,itemBonus);
}
else
Console.WriteLine("You have not sold enough items to recieve the item bonus");
Console.WriteLine("What is the total dollar value of all items sold?");
int bonus1 = 100;
int bonus2 = 200;
int dollarValue = Convert.ToInt16(Console.ReadLine());
double totalEarned1 = (dollarValue * bonus1 + itemBonus);
double totalEarned2 = (dollarValue * bonus2 + itemBonus);
if (dollarValue >= 1000 && dollarValue < 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus1);
}
else if (dollarValue >= 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus2 );
}
else
{
Console.WriteLine("You have not recieved a dollar value bonus");
}
Console.ReadLine();
解决方案
你必须重构你的代码。因为如果用户没有卖出超过 20 件商品,继续计算是没有用的:
Console.WriteLine("What is the total number of items sold?");
int itemsSold = Convert.ToInt16(Console.ReadLine());
int itemBonus = 50;
if (itemsSold > 20)
{
int bonus1 = 100;
int bonus2 = 200;
Console.WriteLine("Your items sold bonus is {0} dollars" ,itemBonus);
Console.WriteLine("What is the total dollar value of all items sold?");
int dollarValue = Convert.ToInt16(Console.ReadLine());
double totalEarned1 = (dollarValue * bonus1 + itemBonus);
double totalEarned2 = (dollarValue * bonus2 + itemBonus);
totalEarned1 = Math.Max( totalEarned1, 230 );
totalEarned2 = Math.Max( totalEarned2, 230 );
if (dollarValue >= 1000 && dollarValue < 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus1);
}
else if (dollarValue >= 5000)
{
Console.WriteLine("You have recieved a bonus of {0} ", bonus2 );
}
else
{
Console.WriteLine("You have not recieved a dollar value bonus");
}
}
else {
Console.WriteLine("You have not sold enough items to recieve the item bonus");
}
Console.ReadLine();
希望这可以帮助。
推荐阅读
- javascript - 无法读取未定义反应js的属性'func'?
- c# - 如何使用 ExecuteScalar() 获取一个值并将其提供给您的 textbox.text?
- postman - 如何在邮递员中将多个变量作为标头请求传递?
- javascript - TypeScript 的无服务器打包问题
- java - 在 javafx 中绘图导致 ClassCastException Double 无法转换为 String
- angular - 使用索引值动态更改 Angular 6 材质特定表行
- javascript - 无法安装 Angular CLI
- jquery - 如何在css中实现flex内容的固定行和滚动行
- odoo - 无法安装 OAuth2 身份验证 odoo12
- scala - 如何为 akka http 的端点引入版本控制