我需要从代码中检索用户输入的帮助

时间:2019-01-20 20:23:42

标签: 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();

1 个答案:

答案 0 :(得分:0)

您必须重构代码。由于如果用户卖出的商品不超过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();

希望这会有所帮助。