using System;
using System.Collections.Generic;
using System.Collections ;
using System.Text;这是头
问题:新建了一个LIST<>
struct N
{
int index;
int value;
int weight;
}
List < N > goods = new List<N>();
然后输入goods.
1 输入goods时,提示框中没有出现goods
2 goods.后边的提示没有出现该问题多次出现,麻烦高手另外指点,这类问题在其它窗口程序中怎么处理?
接着,向goods中输入数据
for (int i = 0; i < count; i++)
{
goods.add();
Console.WriteLine ( "输入元素" );
goods[i] = Console.ReadLine ();
goods[i].index = i;
}
出现多出红色提示,不知道为什么,请大家帮帮忙,谢谢了!
using System.Collections.Generic;
using System.Collections ;
using System.Text;这是头
问题:新建了一个LIST<>
struct N
{
int index;
int value;
int weight;
}
List < N > goods = new List<N>();
然后输入goods.
1 输入goods时,提示框中没有出现goods
2 goods.后边的提示没有出现该问题多次出现,麻烦高手另外指点,这类问题在其它窗口程序中怎么处理?
接着,向goods中输入数据
for (int i = 0; i < count; i++)
{
goods.add();
Console.WriteLine ( "输入元素" );
goods[i] = Console.ReadLine ();
goods[i].index = i;
}
出现多出红色提示,不知道为什么,请大家帮帮忙,谢谢了!
解决方案 »
- c# winform 如何在combobox 中加个树
- WatiN测试框架问题
- 请问怎么使用MessengerClient来得到MSN的好友信息啊?
- 在datalist控件中radiobutton如何实现单选?
- [winform]DataGridView如何一边加载数据一边显示进度条?
- 用C#的directshow的时候出现的转换问题
- 请教各位:如何用c#生成以下的XML文档?
- text_chang事件的触发
- 不用系统函数,怎么编写将十进制转成十六进制与十六进制转成十进制的函数?请写代码?
- 如何判断特殊字符
- [WinForm问题]如何不让程序在托盘中显示?谢谢。
- rowfilter表达式中变量的特殊字符处理
for (int i = 0; i <count ; i++)
{
goods.add();
Console.WriteLine ("输入元素");
goods[i].value;
goods[i].weight;
goods[i].index = i;
}
{
goods.add();//方法Add的A应该大写,且必须接收一个N类型的参数
Console.WriteLine ( "输入元素" );
goods[i] = Console.ReadLine ();//Console.ReadLine()返回string类型,它怎么可能给goods[i]赋值?goods[i]是一个N类型的对象
goods[i].index = i;
}
using System;
using System.Collections.Generic;
using System.Collections ;
using System.Text;
namespace QiongJu
{
class Program
{
static void Main(string[] args)
{
struct N
{
int index;
int value;
int weight;
}
List < N > goods = new List<N>(); //所有的物品
List < N > T = new List<N>(); //存放所有可能的组合
static int m = 0; //静态操作数组 便于操作
static int n = 0;
int bag = 20; //袋子的承重量
static int count ; //物品总数
Console .readline(count);
for (int i = 0; i < count; i++)
{
goods.add();
Console.WriteLine ( "输入元素" );
goods[i] = Console.ReadLine ();
goods[i].index = i;
}
for ( int i = 0; i < count ;i++)
{
s = goods[i].weight ;
if ( bag >= s )
T[m][n] = goods[i].weight ; // 将第i件物品装入
n++;
for (int k = i++; k < count ; k++)
{
for ( int j = k; j < count ; j++)
{
s = s + goods[j].weight ;
if (bag > s)
{
T[m][n] = goods [j].weight ;
n++;
}
else
{
m++;
n = 0;
s = goods [i].weight ;
break ;
}
}
}
}
}
}
}
参考以下写法: struct N
{
public int index;
public int value;
public int weight;
}
static void Main(string[] args)
{
List<N> goods = new List<N>();
int count = 3;
for (int i = 0; i < count; i++)
{
N n = new N();
Console.Write("请输入value:");
n.index = i;
n.value = int.Parse(Console.ReadLine());
Console.Write("请输入weight:");
n.weight = int.Parse(Console.ReadLine());
goods.Add(n);
} foreach (N n in goods)
{
Console.WriteLine("index:{0},value:{1},weight:{2}", n.index, n.value, n.weight);
}
}
using System;
using System.Collections.Generic;
using System.Collections ;
using System.Text;
namespace QiongJu
{
class Program
{
static void Main(string[] args)
{
struct N
{
int index;
int value;
int weight;
}
List < N > goods = new List <N>(); //所有的物品
List < N > T = new List <N>(); //存放所有可能的组合
static int m = 0; //静态操作数组 便于操作
static int n = 0;
int bag = 20; //袋子的承重量
static int count ; //物品总数
Console .readline(count);
for (int i = 0; i < count; i++)
{
goods.add();
Console.WriteLine ( "输入元素" );
goods[i].value = Console.ReadLine ();
goods[i].weight = Console .ReadLine ();
goods[i].index = i;
}
for ( int i = 0; i < count ;i++)
{
s = goods[i].weight ;
if ( bag >= s )
T[m][n] = goods[i].weight ; // 将第i件物品装入
n++;
for (int k = i++; k < count ; k++)
{
for ( int j = k; j < count ; j++)
{
s = s + goods[j].weight ;
if (bag > s)
{
T[m][n] = goods [j].weight ;
n++;
}
else
{
m++;
n = 0;
s = goods [i].weight ;
break ;
}
}
}
}
}
}
} 引用:
system
system.data
system.xml
输入Console.也不能提示后边的,比如writeline readline 等
goods[i].weight = Console .ReadLine (); ReadLine读入的是一个String,需要Parse成int
如下:
string s = Console.ReadLine();
goods[i].value = int.Parse(s);
Goods.Add(goods) //goods為創建好的結構