首页 > 解决方案 > 我需要帮助从我的代码中检索用户输入

问题描述

我只学习了大约一周的 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();

标签: c#

解决方案


你必须重构你的代码。因为如果用户没有卖出超过 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();

希望这可以帮助。


推荐阅读