好的,所以我正在根据我最近的一个学校项目来模拟这个想法,我不确定为什么它现在不起作用。
我目前对该程序的计划是使用 DataManager 来保存一个字符串数组,其中包含用户将从菜单中输入的数字,然后将它们转换为整数,并最终由运算符等通过它们进行描述。我还没有弄清楚最后一部分,我认为这个想法不会像我说的那样奏效。但是现在我无法让程序在数组中存储字符串值。
当我运行程序时,我当前的目标是单击“1”并按“Enter”,当发生这种情况时,我希望输出看起来像“数字:1”,但我一直抛出异常。
“在 mscorlib.dll 中发生了“System.InvalidCastException”类型的未处理异常
附加信息:源数组中的至少一个元素无法转换为目标数组类型。”
这是我的 DataManager 类中的代码....
public class DataManager
{
public ArrayList calculation;
public DataManager()
{
calculation = new ArrayList();
}
public void addNumbers(string n)
{
calculation.Add(n);
}
// public NumbersAndOperators[] getNumbers()
//{
// return //(NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
//}
public void removeNumbers(NumbersAndOperators n)
{
calculation.Remove(n);
}
public void addOperators(string o)
{
calculation.Add(o);
}
public NumbersAndOperators[] getOperators()
{
return (NumbersAndOperators[])calculation.ToArray(typeof(NumbersAndOperators));
}
public void removeOperators(NumbersAndOperators o)
{
calculation.Remove(o);
}
}
}
这是我的 NumbersAndOperators 类的代码
public class NumbersAndOperators
{
private string number; private string operate;
public NumbersAndOperators(string n, string o)
{
number = n;
operate = o;
}
public void setNumber(string n)
{
number = n;
}
public void setOperate(string o)
{
operate = o;
}
public string getNumber(string n)
{
return n;
}
public string getOperate(string o)
{
return o;
}
}
}
这是我表单中的代码
public partial class Form1 : Form
{
public DataManager data;
public Form1(DataManager d)
{
InitializeComponent();
data = d;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
string n = "0";
data.addNumbers(n);
}
private void button1_Click(object sender, EventArgs e)
{
string n = "1";
data.addNumbers(n);
}
private void button2_Click(object sender, EventArgs e)
{
string n = "2";
data.addNumbers(n);
}
private void button3_Click(object sender, EventArgs e)
{
string n = "3";
data.addNumbers(n);
}
private void button4_Click(object sender, EventArgs e)
{
string n = "4";
data.addNumbers(n);
}
private void button5_Click(object sender, EventArgs e)
{
string n = "5";
data.addNumbers(n);
}
private void button6_Click(object sender, EventArgs e)
{
string n = "6";
data.addNumbers(n);
}
private void button7_Click(object sender, EventArgs e)
{
string n = "7";
data.addNumbers(n);
}
private void button8_Click(object sender, EventArgs e)
{
string n = "8";
data.addNumbers(n);
}
private void button9_Click(object sender, EventArgs e)
{
string n = "9";
data.addNumbers(n);
}
private void division_Click(object sender, EventArgs e)
{
string o = "/";
data.addOperators(o);
}
private void multiplication_Click(object sender, EventArgs e)
{
string o = "*";
data.addOperators(o);
}
private void subtraction_Click(object sender, EventArgs e)
{
string o = "-";
data.addOperators(o);
}
private void addition_Click(object sender, EventArgs e)
{
string o = "+";
data.addOperators(o);
}
private void dec_Click(object sender, EventArgs e)
{
string n = ".";
data.addNumbers(n);
}
private void equals_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
NumbersAndOperators[] num = data.getNumbers();
Label tmp;
for (int i = 0; i < num.Length; i++)
{
tmp = new Label();
tmp.Text = "Number:" + data.getNumbers();
tmp.AutoSize = true;
tmp.Location = new Point(0, 85);
panel1.Controls.Add(tmp);
}
}
}
}
我感谢任何和所有建议,这是我以前从未见过的问题,这使得仅阅读就更难理解。我现在像傻瓜一样在谷歌上搜索了一段时间,出现的一切我根本不明白。
另外,我注释掉了抛出异常的行