我正在开发一个应用程序,允许用户跟踪省钱目标。
public static int CalcProg(int userGoal, int userBalance, int userProg)
{
userProg = userBalance / userGoal;
userProg = userProg * 100
return userProg;
}
private void Form1_Load(object sender, EventArgs e)
{
//Calls the FileVerification Method
FileVerification();
//Sets the label1 transparency to true
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
LoadData();
CalcProg(userGoal, userBalance, userProg);
progressBar1.Value = userProg;
progLabel = Convert.ToString(userProg);
label3.Text = progLabel;
}
这只是代码的一小部分,但这是我遇到问题的地方。
我使用方法来读取和写入变量 userBalance 和 userGoal 中使用的数据的文件。这一切都工作正常,因为当我在下面的转换函数中使用这些变量之一而不是“userProg”时,它的显示就像在文本文件中一样。
当我尝试进行转换时,我的问题出现了。我的公式在 CalcProg 中。当我实际启动程序时,在变量 userProg 上设置其值的两个元素(进度条和标签)仅显示零,无论在文本文件中输入什么值。
我尝试对 CalcProg 方法使用 double 型并将 userProg 设置为 double 型,但这不起作用。我有点卡住了,如果有人可以帮助我,我将不胜感激。
最佳答案
实际的问题是,您从方法中返回一个 int,但您从未使用它。 将代码更改为:
public static int CalcProg(int userGoal, int userBalance, int userProg)
{
userProg = userBalance / userGoal;
userProg = userProg * 100
return userProg;
}
private void Form1_Load(object sender, EventArgs e)
{
//Calls the FileVerification Method
FileVerification();
//Sets the label1 transparency to true
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
LoadData();
progressBar1.Value = CalcProg(userGoal, userBalance, userProg);
progLabel = Convert.ToString(userProg);
label3.Text = progLabel;
}
关于c# - 如何正确返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628022/