你如何正确地返回一个值?

emu*_*m13 2 c# function

我正在开发一个允许用户跟踪省钱目标的应用程序.

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;
}
Run Code Online (Sandbox Code Playgroud)

这只是代码的一小部分,但它是我遇到问题的地方.

我使用方法来读取和写入变量userBalance和userGoal中使用的数据的文件.这一切都运行正常,因为当我在下面的转换函数中使用其中一个变量而不是"userProg"时,它就像在文本文件中一样显示.

当我尝试进行转换时,问题出现了.我的公式是CalcProg.当我实际启动程序时,在变量userProg上设置其值的两个元素(进度条和标签),无论在文本文件中输入什么值,都只显示零.

我已经尝试使用双重作为CalcProg方法并将userProg设置为double,但这不起作用.我有点卡住了,如果有人可以帮助我,我会很感激.

Hes*_*her 5

实际问题是,您从方法返回一个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;


}
Run Code Online (Sandbox Code Playgroud)