如何在每年增加时增加一个 var?
How to increment a var everytime the year increases?
在我的程序中,我有一个车辆的一般价格估算器。如果年份低于 1990,则价格设置为 1000 美元。但是,如果每增加一年,年份增加一年,则估算值应额外增加 200 美元。例如,如果年份是 1991 年,那么价格应该是 1200 美元。
我曾尝试在 if 语句中递增 Year 并将 200 添加到用于设置价格的变量中,但这不起作用。我还尝试使用 for 循环,但也不成功。
public decimal DetermineMarketValue()
{
decimal carValue;
if (Year < 1990)
carValue = 1000;
if (Year > 1990)
for (Year++)
{
carValue += 200;
}
return carValue;
}
每年每增加一年,估计就会增加 200 美元。
如果你必须使用循环,认为你必须使用 while 循环而不是 for:
public static decimal DetermineMarketValue(int Year)
{
decimal carValue = 1000;
while (Year > 1990)
{
carValue += 200;
Year--;
}
return carValue;
}
我认为你没有初始化 carValue
编译器会崩溃,但是,你需要用默认值初始化。
In my opinion, don't use loops if you have a simple calculation like this.
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.
我很欣赏评论中提出的观点,但我也认为这是一个很好的介绍循环逻辑的学术练习,所以我会坚持使用循环解决方案。你正在学习并且似乎处于早期阶段 - 我怎么强调在尝试编写代码之前用英语(或你的母语)编写算法的重要性都不为过。你一生都在用英语思考,现在你正在学习一种新语言,它有严格的规则和对精确性的高要求。就像你在说法语,你首先 assemble 你想用英语说的句子(单词按法语顺序)然后你翻译成法语,然后你说法语。您需要很长时间才能用法语思考
代码相同;思考英语,写英语评论,翻译成 c#,瞧;评论很好的代码(加分)
在我的程序中,我有一个车辆的一般价格估算器。如果年份低于 1990,则价格设置为 1000 美元。但是,如果每增加一年,年份增加一年,则估算值应额外增加 200 美元。例如,如果年份是 1991 年,那么价格应该是 1200 美元。
我曾尝试在 if 语句中递增 Year 并将 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;
}
这段代码有一个故意的错误 - 我不是来这里为你做学位的,所以我犯了一个公然和故意的错误,部分是为了让你考虑编写的代码,部分是为了强调在注释中编写算法时如何检查代码是否与注释匹配
在我的程序中,我有一个车辆的一般价格估算器。如果年份低于 1990,则价格设置为 1000 美元。但是,如果每增加一年,年份增加一年,则估算值应额外增加 200 美元。例如,如果年份是 1991 年,那么价格应该是 1200 美元。
我曾尝试在 if 语句中递增 Year 并将 200 添加到用于设置价格的变量中,但这不起作用。我还尝试使用 for 循环,但也不成功。
public decimal DetermineMarketValue()
{
decimal carValue;
if (Year < 1990)
carValue = 1000;
if (Year > 1990)
for (Year++)
{
carValue += 200;
}
return carValue;
}
每年每增加一年,估计就会增加 200 美元。
如果你必须使用循环,认为你必须使用 while 循环而不是 for:
public static decimal DetermineMarketValue(int Year)
{
decimal carValue = 1000;
while (Year > 1990)
{
carValue += 200;
Year--;
}
return carValue;
}
我认为你没有初始化 carValue
编译器会崩溃,但是,你需要用默认值初始化。
In my opinion, don't use loops if you have a simple calculation like this.
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.
我很欣赏评论中提出的观点,但我也认为这是一个很好的介绍循环逻辑的学术练习,所以我会坚持使用循环解决方案。你正在学习并且似乎处于早期阶段 - 我怎么强调在尝试编写代码之前用英语(或你的母语)编写算法的重要性都不为过。你一生都在用英语思考,现在你正在学习一种新语言,它有严格的规则和对精确性的高要求。就像你在说法语,你首先 assemble 你想用英语说的句子(单词按法语顺序)然后你翻译成法语,然后你说法语。您需要很长时间才能用法语思考
代码相同;思考英语,写英语评论,翻译成 c#,瞧;评论很好的代码(加分)
在我的程序中,我有一个车辆的一般价格估算器。如果年份低于 1990,则价格设置为 1000 美元。但是,如果每增加一年,年份增加一年,则估算值应额外增加 200 美元。例如,如果年份是 1991 年,那么价格应该是 1200 美元。
我曾尝试在 if 语句中递增 Year 并将 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;
}
这段代码有一个故意的错误 - 我不是来这里为你做学位的,所以我犯了一个公然和故意的错误,部分是为了让你考虑编写的代码,部分是为了强调在注释中编写算法时如何检查代码是否与注释匹配