我的错误消息说“无法将类型double转换为int []”,与string
和int
相同(最后,arraySalsa
)我理解为什么,但我是不确定如何修复所以到目前为止数组将全部带入。你能帮忙吗?
class Program
{
static void Main(string[] args)
{
string[] heat = new string[] { "Mild", "Medium", "Hot", "Super Hot", "Scorching" };
double[] price = new double[4];
int[] numSold = new int[4];
int counter;
{
for (counter = 0; counter != heat.Length; counter++)
{
Console.WriteLine("What do you wish to charge for " + heat[counter] + " ?");
price[counter] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("How many jars of " + heat[counter] + " did you sell last year?");
numSold[counter] = Convert.ToInt32(Console.ReadLine());
}//end forloop
}
for (int count = 0; count < heat.Length; count++)
{
Console.WriteLine("You sold " + numSold[count] + " of " + heat[count] + " at $" + price[count] + " each.");
}//end forloop
Array[,,] arraySalsa;
arraySalsa = new Array[heat, price, numSold];
}
}
答案 0 :(得分:1)
正如我的评论中所提到的,你应该使用一个类,而不是一个多维数组。
class Program
{
static void Main(string[] args)
{
var measurements = new Measurements();
measurements.Heat = new string[]
{ "Mild", "Medium", "Hot", "Super Hot", "Scorching" }.ToList();
measurements.Price = new List<double>(4);
measurements.NumSold = new List<int>(4);
int counter;
{
for (counter = 0; counter != measurements.Heat.Count; counter++)
{
Console.WriteLine("What do you wish to charge for " +
measurements.Heat[counter] + " ?");
measurements.Price[counter] = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("How many jars of " + measurements.Heat[counter] +
" did you sell last year?");
measurements.NumSold[counter] = Convert.ToInt32(Console.ReadLine());
}
}
for (int count = 0; count < measurements.Heat.Count; count++)
{
Console.WriteLine("You sold " + measurements.NumSold[count] + " of " +
measurements.Heat[count] + " at $" + measurements.Price[count]
+ " each.");
}
// your arraySalsa is now just your measurements instance
}
}
public class Measurements
{
public List<string> Heat { get; set; }
public List<double> Price { get; set; }
public List<int> NumSold { get; set; }
}
在C#中使用列表而不是数组有很多好处,即使在进行简单索引时也是如此。由于它们是为您动态调整大小的,因此您不必担心增加上限。此外,您可以使用强大的LINQ
方法来帮助您更轻松地操作集合。如果可能(除非您有极端的性能考虑因素),您应该始终倾向于列表。
或者,您可以创建一个包含三个属性中每个属性的Measurement类,并创建该类的集合,以清理所有索引操作。
class Program
{
static void Main(string[] args)
{
var measurements = new List<Measurement>();
var heat = new string[]
{ "Mild", "Medium", "Hot", "Super Hot", "Scorching" }.ToList();
int counter;
{
for (counter = 0; counter != heat.Count; counter++)
{
var measurement = new Measurement() { Heat = heat[counter]};
Console.WriteLine("What do you wish to charge for " +
measurement.Heat + " ?");
measurement.Price = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("How many jars of " + measurement.Heat +
" did you sell last year?");
measurement.NumSold = Convert.ToInt32(Console.ReadLine());
measurements.Add(measurement);
}
}
measurements.ForEach(x =>
{
Console.WriteLine("You sold " + x.NumSold + " of "
+ x.Heat + " at $" + x.Price + " each.");
});
}
}
public class Measurement
{
public string Heat { get; set; }
public double Price { get; set; }
public int NumSold { get; set; }
}
答案 1 :(得分:0)
如果你想在数组(或List或其他)中存储不同的类型,并且它们没有公共基类,那么你需要将它们存储为盒装对象。
var salsa = new object[20];
salsa[0] = 1;
salsa[1] = 42.42;
salsa[3] = "hot";
阅读它们你需要做的
var s1 = salsa[1];
if (s1 is int)
Console.WriteLine((int)s1);
if(s1 is double)
COnsole.WriteLine((double)s1);
etc...
请参阅How to unbox from object to type it contains, not knowing that type at compile time?
答案 2 :(得分:0)
一种选择是使用object
s:
object[] arraySalsa;
arraySalsa = new object[] {(object)heat, (object)price, (object)numSold};
如果需要,可以省略类型转换操作符(object)
,编译器会推断它应该进行类型转换:
object[] arraySalsa;
arraySalsa = new object[] {heat, price, numSold};
然而,使用object[]
可能是不好的做法,因为只给出了object[]
类型,你不知道它中包含什么,编译器不会停止你不小心把错误的东西放在里面。因此,使用Tuple
:
Tuple<string[], double[], int[]> tupleSalsa;
tupleSalsa = new Tuple<string[], double[], int[]>(heat, price, numSold);
为方便起见,您可以使用Tuple.Create
代替new Tuple<string[], double[], int[]>
,这样您就不必再次写出该类型了:
Tuple<string[], double[], int[]> tupleSalsa;
tupleSalsa = Tuple.Create(heat, price, numSold);
答案 3 :(得分:0)
创建多个类型数组的漏洞思路是错误的(打破数据一致性)。如果你按照建议使用对象,你将有很多拳击/拆箱来做后者。我建议创建一个具有3个属性的类产品:温度,价格,数量和数量。使用产品清单......