C# 如何在每年增加时增加var?

C# 如何在每年增加时增加var?,c#,increment,C#,Increment,在我的程序中,我有一个通用的车辆价格估计器。如果年份低于1990年,价格定为1000美元。但是,如果每年增加一年,估计数应该增加200美元。例如,如果年份是1991年,那么价格应该是1200美元 我曾尝试在if语句中增加年份,并将200添加到用于设置价格的变量中,但这不起作用。我还尝试使用for循环,但也没有成功 public decimal DetermineMarketValue() { decimal carValue; if

在我的程序中,我有一个通用的车辆价格估计器。如果年份低于1990年,价格定为1000美元。但是,如果每年增加一年,估计数应该增加200美元。例如,如果年份是1991年,那么价格应该是1200美元

我曾尝试在if语句中增加年份,并将200添加到用于设置价格的变量中,但这不起作用。我还尝试使用for循环,但也没有成功

public decimal DetermineMarketValue()
        {
            decimal carValue;
            if (Year < 1990)
                carValue = 1000;
            if (Year > 1990)
                for (Year++)
                {
                    carValue += 200;     
                }

            return carValue;

        }
    //this should take in the year as a parameter to determine for 
    public int DetermineMarketValue(int forYear)
    {
        //pick 1000 as the starting value 
        int carValue = 1000;

        //if the user asked for earlier than 1990, quit early 
        if (forYear < 1990)
            return carValue;

        //they must have asked for a year after 1990, let's do the math 

        //start with 1990, test if we should add to the value, go up a year and test if we should add again
        //this logic starts with 1990 and if the user asked for 1990 the loop will not run, 1991 it UBS once etc
        //if the logic should be that a 1990 car is worth 1200, make it <= not <
        for (int year = 1990; year < forYear; year++)
        {
                //add 200 dollars onto the value and test
                carValue -= 200;     
        }

         //the loop has run the required number of times to get the final value, return it
        return carValue;

    }
public decimal determinitemarketvalue()
{
十进制值;
如果(1990年以前)
carValue=1000;
如果(年份>1990年)
年++)
{
carValue+=200;
}
返回值;
}

每一年增加一次,估计值应增加200美元。

如果必须使用循环,请认为必须使用while循环,而不是用于:

public static decimal DetermineMarketValue(int Year)
        {
            decimal carValue = 1000;

            while (Year > 1990)
            {
                carValue += 200;
                Year--;
            }

            return carValue;

        }

我认为您没有初始化
carValue
编译器将崩溃,但是,您需要使用默认值初始化

在我看来,如果你有这样一个简单的计算,就不要使用循环


参阅结果。

我欣赏评论中提出的观点,但我也认为这是一个对循环逻辑很好的介绍的学术练习,所以我将坚持循环解决方案。您正在学习,而且似乎还处于早期阶段——在尝试编写代码之前,我再怎么强调用英语(或您的母语)编写算法的重要性也不为过。你一生都在用英语思考,现在你正在学习一门新的语言,它有着严格的规则和对精确性的高要求。就像你在说法语一样,你首先用英语拼凑出你想说的句子(用法语顺序),然后翻译成法语,然后说法语。你要花很长时间才能用法语思考

代码相同;思考英语,写英语评论,翻译成c#,瞧;注释良好的代码(额外积分)

在我的程序中,我有一个通用的车辆价格估计器。如果年份低于1990年,价格定为1000美元。但是,如果每年增加一年,估计数应该增加200美元。例如,如果年份是1991年,那么价格应该是1200美元

我曾尝试在if语句中增加年份,并将200添加到用于设置价格的变量中,但这不起作用。我还尝试使用for循环,但也没有成功

public decimal DetermineMarketValue()
        {
            decimal carValue;
            if (Year < 1990)
                carValue = 1000;
            if (Year > 1990)
                for (Year++)
                {
                    carValue += 200;     
                }

            return carValue;

        }
    //this should take in the year as a parameter to determine for 
    public int DetermineMarketValue(int forYear)
    {
        //pick 1000 as the starting value 
        int carValue = 1000;

        //if the user asked for earlier than 1990, quit early 
        if (forYear < 1990)
            return carValue;

        //they must have asked for a year after 1990, let's do the math 

        //start with 1990, test if we should add to the value, go up a year and test if we should add again
        //this logic starts with 1990 and if the user asked for 1990 the loop will not run, 1991 it UBS once etc
        //if the logic should be that a 1990 car is worth 1200, make it <= not <
        for (int year = 1990; year < forYear; year++)
        {
                //add 200 dollars onto the value and test
                carValue -= 200;     
        }

         //the loop has run the required number of times to get the final value, return it
        return carValue;

    }
//这应该以年份作为参数来确定
公共整数确定市场价值(整数年)
{
//选择1000作为起始值
int carValue=1000;
//如果用户在1990年之前请求,请提前退出
如果(1990年以前)
返回值;
//他们一定要求在1990年后一年,让我们算算吧
//从1990年开始,测试我们是否应该增加价值,上升一年,测试我们是否应该再次增加价值
//这个逻辑从1990年开始,如果用户要求1990年,循环将不会运行,1991年它将运行一次,以此类推

//如果逻辑应该是一辆1990年的车价值1200美元,那就让它成为你不需要一个回路。这是一个非常基本的数学问题。1990年每年200美元。
carValue=1000+(year-1990)*200目前来看,如果汽车是1990年制造的,它的价值为0美元。假设1990年为1000美元,1990年后每年为200美元,
carValue=1000+(年份-1990)*200;
谢谢大家的回答!谢谢大家,这是解决这个问题的最简单的方法。不排除其他答案,它们也是合理的。@jGaines这远不是最简单的方法。这是你要求的,但编程相当于用手指数到5。你收到的评论中的代码ed实际上更简单(一行),并且具有自我解释的优势。该代码有一个严重缺陷,忽略了一个问题前提。我认为这个答案比公认的答案好,也许我会在if子句之外声明int numYears,并去掉(numYears*200)上的括号,但它比使用while循环进行如此简单的计算要好得多。计算可以如此简单
1000+(Math.Max(year,1990)-1990)*200