C#减肥计算器

时间:2014-02-14 18:06:41

标签: c# if-statement while-loop counter

您好我正在努力使我的代码数量增加10,15,20,25 .... 60然后每次计数3.9cal并显示两者但是由于某种原因我的代码不会倒数你能帮助我吗?

请保持代码简单,因为我还在学习:)

double lost;
int counter = 5;
while (counter <= 60)   
{
  if (counter * 5 == 0)
  {
    lost = counter * 3.9;
    Console.WriteLine("Minutes spend: {0}    Fat Lost: {1}", counter, lost);
    counter++;
  }

  Console.ReadLine();
}

5 个答案:

答案 0 :(得分:3)

你的代码的问题是你的IF语句永远不会成为现实,因为你开始用counter = 5开始你的循环

你需要在if语句之外移动你的++来解决问题

double lost;
int counter = 5;
while (counter <= 60)
{
    if (counter % 5 == 0)
    {
        lost = counter * 3.9;
        Console.WriteLine("Minutes spend: {0}    Fat Lost: {1}", counter,     lost);
    }
    counter++;
}
Console.ReadLine();

答案 1 :(得分:1)

试试这个(如果我理解正确的话):

double lost;
int counter = 5;
while (counter <= 60)   
{
  if (counter % 5 == 0)
  {
    lost = counter * 3.9;
    Console.WriteLine("Minutes spend: {0}    Fat Lost: {1}", counter, lost);            
  }
  counter++;
  Console.ReadLine();
}

答案 2 :(得分:0)

这可以是一个简单的for循环,如:

for(int i = 0; i <= 60; i+=5) 
        Console.WriteLine("Minutes spend: {0} - Fat Lost: {1}", i, i*3.9);

但是,如果您愿意,可以使用LINQ / Enumerable方法。我通常会发现它们更适合这种性质的循环:

var a = Enumerable.Range(1, 12).Select(i => new {Minutes = i * 5, Lost = i * 5 * 3.9});
foreach(var entry in a)
    Console.WriteLine ("Minutes spent: {0} - Fat Lost: {1}", entry.Minutes, entry.Lost);

答案 3 :(得分:0)

其他答案很好地解释了如何修复代码,但这是一种简单循环的方法。

for (int minute = 10; minute <= 60; minute += 5)
    Console.WriteLine("Minutes spend: {0}    Fat Lost: {1}", minute, minute * 3.9);
Console.ReadLine();

答案 4 :(得分:0)

如果您只是尝试计数5,并将计数器乘以3.9,您可以这样做:

double lost;
int counter = 5;
while (counter <= 60)   
{
  lost = counter * 3.9;
  Console.WriteLine("Minutes spend: {0}    Fat Lost: {1}", counter, lost);            
  counter += 5;
  Console.ReadLine();
}