0

几天前我刚刚开始编写 C#,需要一些关于我正在编写的程序的帮助。

这不是真正的代码,只是我写给你看原始代码是如何构建的。

public partial class form1 : form
{
int container;
int resource;
int capacity;
int price;
int total;

int basket = 10; //container that holds 10 slots
int apples = 2; //resource that takes 2 slots per resource
}

public form1()
{
private void checkbox1_checkedchanged(object sender, eventargs e) //basket checkbox
{

if (((CheckBox)checkbox1).Checked == true)
{
basket = container;
}

}

private void checkbox2_checkedchanged(object sender, eventargs e) //apples checkbox
{

if (((CheckBox)checkbox2.Checked == true)
{

apples = resource;

}

}
private void button1_Click(object sender, EventArgs e) //calculate button
{
container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
capacity * price = total;
textbox2.AppendText(total); //textbox2 is where they will se how much the apples cost
}



}

问题是我从“button1”中的代码中得到错误,该代码说明了所有内容

  • 赋值的左侧必须是变量、属性或索引器
  • 无法获取托管类型的地址、大小或声明指向托管类型(“容量”)的指针
  • “WindowsFormApplication1.Form1.Capacity”是一个“字段”,但用作“类型”
  • 指针是固定大小的缓冲区,只能在不安全的上下文中使用
  • 不能将类型“int”隐式转换为“容量*”。存在显式转换(您是否缺少演员表?)
  • 'System.Window.Forms.TextBoxBase.AppendText(String)'的最佳重载方法匹配有一些无效参数
  • 参数 1:无法从 'int' 转换为 'string'

所以我想知道我做错了什么来实现我想要的那种应用程序。该应用程序看起来像这样:

在此处输入图像描述

编辑:原始代码已被删除

Edit2:这就是button1现在的样子,除了我尝试用盐划分车辆时发生的错误(没有尝试过任何其他资源,但就是列出的那个)之外,没有错误。代码看起来和以前一样,但感谢大卫皮尔金顿,我改变了这一点。

private void button1_Click(object sender, EventArgs e) /* calculate-button */
    {
        capacity = vehicle / resource;
        total = capacity * price;
        textBox4.AppendText(total.ToString());
    }
4

2 回答 2

0
private void button1_Click(object sender, EventArgs e) //calculate button
{
    capacity = container / resource; //to see how many resources the basket can hold
    price = int.Parse(textbox1.text); 
    total= capacity * price;
    textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the     apples cost
}

您的作业是错误的,您需要在添加之前将其转换int为 astring

您可能还想TryParse用来处理杂乱的输入

private void button1_Click(object sender, EventArgs e) //calculate button
{
    capacity = container / resource; //to see how many resources the basket can hold
    if(int.TryParse(textbox1.text, out price)) 
    {
       total= capacity * price;
       textbox2.AppendText(total.ToString()); //textbox2 is where they will se how much the apples cost
    }
}

大编辑:

看来你的作业都是错误的。这意味着永远不会设置资源,并且将在此行中创建并出错capacity = container / resource;

于 2014-03-05T09:06:21.697 回答
0

您的代码在 button2_click() 中都是错误的

container / resource = capacity; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
capacity * price = total;
textbox2.AppendText(total); 

应该(也许?)

capacity = container / resource; //to see how many resources the basket can hold
/* textbox 1 is where they write the price for what 1 apple cost */
price = int.Parse(textbox1.text); 
total = capacity * price;
textbox2.AppendText(total.ToString());

这应该可以消除代码中的大部分错误。

希望能帮助到你!//KH。

于 2014-03-05T09:11:35.440 回答