【问题标题】:How to increment a var everytime the year increases?每年增加时如何增加 var?
【发布时间】:2019-06-14 18:56:33
【问题描述】:

在我的程序中,我有一个车辆的通用价格估算器。如果年份低于 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;

        }

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

【问题讨论】:

  • 你不需要循环。这是一个非常基本的数学问题。 1990 年每年 200 美元。carValue = 1000 + (Year - 1990) * 200
  • 就目前而言,如果这辆车是 1990 年制造的,它的价值是 0 美元。假设 1990 年为 1,000 美元,1990 年之后每年为 200 美元,carValue = 1000 + (Year - 1990) * 200;
  • 谢谢大家的回答!

标签: c# increment


【解决方案1】:

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

在我看来,如果您有这样的简单计算,请不要使用循环。

public class Program
{
    public static void Main()
    {
        Console.WriteLine(DetermineMarketValue(2019));
    }

    public static double DetermineMarketValue(int Year)
    {
        double carValue = 1000;
        if (Year > 1990)
        {
            int numYears = Math.Abs(Year - 1990);
            carValue = 1000 + (numYears * 200);
        }
        return carValue;
    }
}

查看结果here

【讨论】:

  • 我认为这个答案比公认的要好,也许我会在 if 子句之外声明 int numYears 并丢失 (numYears*200) 上的括号,但它比使用 while 循环要好得多这么简单的计算。
  • 计算可以这么简单1000 + (Math.Max(year, 1990) - 1990) * 200
【解决方案2】:

我很欣赏 cmets 中提出的观点,但我也认为这是一个学术练习,是循环逻辑的一个很好的介绍,所以我会坚持循环解决方案。您正在学习并且似乎处于早期阶段 - 在尝试编写代码之前,我不能足够强调用英语(或您的母语)编写算法的重要性。你一生都在用英语思考,现在你正在学习一种新的语言,它有严格的规则和对精确度的高要求。就像你在说法语一样,你首先用英语组装你想说的句子(单词按法语顺序),然​​后翻译成法语,然后你说法语。你需要很长时间才能用法语思考

代码相同;想英文,写英文cmets,翻译成c#,瞧;注释很好的代码(加分)

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

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

    //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;

    }

这段代码有一个故意的错误 - 我不是来为你完成学位的,所以我犯了一个明显而故意的错误,部分原因是为了让你思考编写的代码部分是为了强调在 cmets 中编写算法时如何检查代码是否与 cmets 匹配

【讨论】:

    【解决方案3】:

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

    public static decimal DetermineMarketValue(int Year)
            {
                decimal carValue = 1000;
    
                while (Year > 1990)
                {
                    carValue += 200;
                    Year--;
                }
    
                return carValue;
    
            }
    

    【讨论】:

    • 谢谢,这是解决问题的最简单方法。没有从其他答案中删除,它们也是合理的。
    • @jGaines 这远非最简单的解决方案。这是您所要求的,但它相当于用手指数到 5 的编程。您收到的 cmets 中的代码实际上更简单(一行)并且具有不言自明的优势。
    • 这段代码有一个严重的缺陷,忽略了问题前提之一
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多