如何在if语句内已声明的if语句外使用变量 [英] how to use a variable outside of an if statement that is already declared inside an if statement

查看:133
本文介绍了如何在if语句内已声明的if语句外使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我只是在学校里学习C#,当我已经在if语句中声明了该变量时,很难弄清楚我该如何在if语句之外使用变量.这就是我的程序的样子........我必须在if陈述式之外使用"factor"变量,因为它是我进行学校作业所需要的方程式的一部分..如果我缺少任何内容或您需要更多信息,请别犹豫告诉我

hey guys im just learning C# in school and am having trouble figuring out how i can use a variable outside of an if statement when that variable is already declared inside an if statement..heres what my program looks like........i have to use the "factor" variable outside of the if statemants because it is part of an equation that i need for a school assignment..if i am missing anything or you need more information, plz dont hesitate to tell me

 public caloriesCalculator()
        {
            InitializeComponent();
        }

        private void calculate_Click(object sender, EventArgs e)
        {
            double kilos;
            double pounds;
            int factor;
            double totalcalories;
            pounds = double.Parse(lbsTxt.Text);
            kilos = pounds * 0.45;
            kilosTxt.Text = kilos.ToString();


            {

                if (maleRadio.Checked && activeRadio.Checked)
                {
                    factor = 15;
                }

                if (femaleRadio.Checked && activeRadio.Checked)
                {
                    factor = 12;
                }

                if (maleRadio.Checked && inactiveRadio.Checked)
                {
                    factor = 13;
                }

                if (femaleRadio.Checked && inactiveRadio.Checked)
                {
                    factor = 10;
                }
            }
        }

推荐答案

不清楚:但是您是说这不能编译吗?在进入if语句之前,您需要分配一个值(默认值).

Not clear: But are you saying that this doesn't compile? You need to assign a value (a default) before going inside the if statement.

int factor = 15;

这篇关于如何在if语句内已声明的if语句外使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆