C#使用方法的问题

C#使用方法的问题,c#,C#,我被指派用不同的方法计算医院费用。我已经弄明白了其中的大部分,但有一部分我被卡住了。当我尝试使用另一个方法中的变量时,该值似乎不会移到新方法中。正确的做法是什么?我对CalcTotalCharges方法有意见 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using Sy

我被指派用不同的方法计算医院费用。我已经弄明白了其中的大部分,但有一部分我被卡住了。当我尝试使用另一个方法中的变量时,该值似乎不会移到新方法中。正确的做法是什么?我对CalcTotalCharges方法有意见

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hospitalBills
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {

        }
        private void enter_Click(object sender, EventArgs e)
        {
            int dayStayd = int.Parse(dayStay.Text);
            int medFee = int.Parse(medCharge.Text);
            int surgFee = int.Parse(surgCharges.Text);
            int labFee = int.Parse(labCharges.Text);
            int rhbFee = int.Parse(rhbCharges.Text);
            CalcStayCharge(dayStayd);
            CalcMiscCharges(medFee, surgFee, labFee, rhbFee);
            CalcTotalCharges(totalFee,stayCost);
            total.Text = totalCost.ToString();

        }
        public int CalcStayCharge(int dayStayd)
        {
            int stayCost = dayStayd * 350;
            return stayCost;
        }
        public int CalcMiscCharges(int medFee, int surgFee, int labFee, int rhbFee)
        {
            int totalFee = medFee + surgFee + labFee + rhbFee;
            return totalFee;
        }
        public int CalcTotalCharges(int totalFee, int stayCost)
        {
            int totalCost = totalFee + stayCost;
            return totalCost;
        }
    }
}

正如@MethodMan在他的评论中所说的,您的函数“工作”,但您需要在变量中捕获输出以使用它们。请参见下面的示例,了解如何执行此操作

private void enter_Click(object sender, EventArgs e)
{
    int dayStayd = int.Parse(dayStay.Text);
    int medFee = int.Parse(medCharge.Text);
    int surgFee = int.Parse(surgCharges.Text);
    int labFee = int.Parse(labCharges.Text);
    int rhbFee = int.Parse(rhbCharges.Text);
    var stayCost = CalcStayCharge(dayStayd);
    var totalFee = CalcMiscCharges(medFee, surgFee, labFee, rhbFee);
    var totalCost = CalcTotalCharges(totalFee,stayCost);
    total.Text = totalCost.ToString();
}

调用返回数据类型的方法时,例如
int
执行以下
var someclacstaychage=CalcStayCharge(dayStayd)例如,对其他2种方法执行相同操作。。您正在返回一个Int,但从未真正将其捕获/分配给要在
enter\u Click
事件范围内使用的局部变量。如果需要,您还可以替换局部变量并将其转换为自动属性,并将Int值存储在那里。。有几种方法可以解决这个问题。。但现在,您所做的只是调用一个方法并返回一些永远不会返回的内容captured@Ben很明显,可以看到什么方法起作用,但是OP对如何调用该方法的期望不起作用
CalcTotalCharges(totalFee,stayCost);total.Text=totalCost.ToString()
@TacosaurusRex我建议您在google上搜索以下
void方法和返回值的方法
,这将帮助您了解如何调用返回值的方法来捕获和分配它们的值我立即看到了这一点,但我想给OP一个机会来思考如何将返回值分配给变量+1