C#使用方法的问题

本文关键字:问题 使用方法 | 更新日期: 2023-09-27 18:19:54

我被指派用不同的方法计算医院费用。大部分我都想明白了,但有一部分我被卡住了。当我尝试使用另一个方法中的变量时,值似乎不会转移到新方法。做这件事的正确方法是什么?我对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;
        }
    }
}

C#使用方法的问题

正如@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();
}