列表的属性<>没有更新 [英] Property of a list<> not updating

查看:56
本文介绍了列表的属性<>没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

I have this code below:

Foood[] food = new Foood[types];
            food[0] = new Foood("protein", 2.0);
            food[1] = new Foood("vitamins", 3.0);
            food[2] = new Foood("Carbonhydrate", 4.0);



然后这个


and then this

List<Foood> foods = new List<Foood>();

     foods.Add(food[0]);
     foods.Add(food[1]);
     foods.Add(food[2]);







然后这个




and then this

for (int i = 0; i < days; i++)
{

    UpdateCalories(foods);

}










public void UpdateCalories(List<Foood> foodItem)
        {

            for (int i = 0; i < foodItem.Count; i++)
            {
                foodItem[i].calories = foodItem[i].calories + 1.1;
            }
        }





i希望在打印前能够获得不同日期的价值



我尝试了什么:



我已经编写并粘贴了代码



i want to be able to have values for different days before printing

What I have tried:

I have written and pasted the code

got this:
Day0
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day1
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day2
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

Day3
protein,5.3
vitamins,6.3
Carbonhydrate,7.3
----------------------------------------



但我想要这个:


But I want this:

Day0
protein,2.0
vitamins,3.0
Carbonhydrate,4.0

Day1
protein,3.1
vitamins,4.1
Carbonhydrate,5.1

Day2
protein,4.2
vitamins,5.2
Carbonhydrate,6.2

Day3
protein,5.3
vitamins,6.3
Carbonhydrate,7.3

-----------------



我仍​​然希望能够访问前几天的值而不仅仅是最后一天。



这是我的输出代码

----------------------------


I still want to have access to the values of the previous days not just the last day.

this is my output code
----------------------------

using (StreamWriter sw = new StreamWriter(@"U:\Food\food.txt"))
{
	sw.WriteLine("Day" + "{0}", 0);
	for (int ii = 0; ii < types; ii++)
	{
		sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
	}
	sw.WriteLine("");
	for (int i = 0; i < days; i++)
	{
		{
			sw.WriteLine("Day" + "{0}", i + 1);
			for (int ii = 0; ii < types; ii++)
			{
				sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
			}
			sw.WriteLine("");
		}
	}
}

推荐答案

public class Foood
       {
           public string Type { get; set; }
           public double Calories { get; set; }

           public Foood(string type, double calories)
           {
               Type = type;
               Calories = calories;
           }
       }

       public class Days
       {
          public List<Foood> foods = new List<Foood>();
       }

       private void testit()
       {
           int types = 3;
           List<Foood> foods = new List<Foood>();
           List<Days> foodDays = new List<Days>();
           int days = 3;


           Foood[] food = new Foood[types];
           food[0] = new Foood("protein", 2.0);
           food[1] = new Foood("vitamins", 3.0);
           food[2] = new Foood("Carbonhydrate", 4.0);

           for (int i = 0; i < food.Length; i++)
           {
               foods.Add(food[i]);
           }

           Days day = new Days();
           day.foods = foods;
           foodDays.Add(day);

           for (int i = 1; i <= days; i++)
           {
               day = new Days();
               day.foods = UpdateCalories(foodDays[i-1].foods);
               foodDays.Add(day);
           }

       }

       public List<Foood> UpdateCalories(List<Foood> foodItem)
       {
           List<Foood> newFood = new List<Foood>();
           Foood item;

           for (int i = 0; i < foodItem.Count; i++)
           {
               item = new Foood(foodItem[i].Type, foodItem[i].Calories + 1.1);
               newFood.Add(item);
           }

           return newFood;
       }
   }


试试这段代码

try this code
using (StreamWriter sw = new StreamWriter(@"U:\Food\food.txt"))
{
	for (int i = 0; i < days; i++)
	{
		sw.WriteLine("Day" + "{0}", i);
		for (int ii = 0; ii < types; ii++)
		{
			sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories);
		}
		sw.WriteLine("");
		//	UpdateCalories here
	}
}





[更新]

另一种看待问题的方法是:

第0天,使用物超所值

第1天,使用好价值+ 1.1

第2天,使用好价值+ 1.1 * 2

第3天,使用好价值+ 1.1 * 3

...

你不会改变食物价值。



[Update]
Another way to see the problem is:
day 0, use good values
day 1, use good values + 1.1
day 2, use good values + 1.1 * 2
day 3, use good values + 1.1 * 3
...
and you don't change food values.

sw.WriteLine("{0},{1}", food[ii].Type, food[ii].Calories + 1.1 * i);


这篇关于列表的属性&lt;&gt;没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆