(1)return方法求平方值
代码如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
A a = new A();
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt16(textBox1 .Text );
label1.Text = "原值:"+Convert.ToString(x);
int x2 = a.B(x);
label1.Text += "\n\n\n" + "平方值:" + Convert.ToString(x2);
}
}
public class A{
public int B(int i)
{
int b = i*i;
i = b;
return i;
}
}
通过return方法我们可以看到需要在方法体写入return语句,方法体返回值为int,还需要定义一个同类型的变量接收方法的返回值。