这是什么[使用未分配的局部变量&QUOT的;意思? [英] What does "Use of unassigned local variable" mean?

查看:137
本文介绍了这是什么[使用未分配的局部变量&QUOT的;意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到此错误annualRate,monthlyCharge和lateFee..And为我的死我想不通为什么。任何帮助将大大appriciated!

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;命名空间Lab_5___Danny_Curro
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            字符串的firstName;
            字符串的lastName;
            INT accNumber;
            串creditPlan;
            双平衡;
            字符串状态;
            布尔晚= FALSE;
            双lateFee;
            双monthlyCharge;
            双annualRate;
            双netBalance;            Console.Write(输入名字:);
            的firstName =到Console.ReadLine();            Console.Write(输入姓氏:);
            lastName的=到Console.ReadLine();            Console.Write(输入帐号:);
            accNumber = Convert.ToInt32(到Console.ReadLine());
            Console.Write(输入信用卡计划编号[空白将进入计划0]:);
            creditPlan =到Console.ReadLine();            Console.Write(输入余额:);
            余额= Convert.ToDouble(到Console.ReadLine());            Console.Write(该帐户晚?:);
            状态=到Console.ReadLine()修剪()ToLower将()。            如果(creditPlan ==0)
            {
                annualRate = 0.35; // 35%
                lateFee = 0.0;
                monthlyCharge =平衡*(annualRate *(1/12));
                返回;
            }            如果(creditPlan ==1)
            {
                annualRate = 0.30; // 30%
                如果(状态==Y)
                {
                    晚=真;
                }                否则如果(状态==n个)
                {
                    晚= FALSE;
                }
                如果(晚==真)
                {
                    lateFee = 25.00;
                }
                monthlyCharge =平衡*(annualRate *(1/12));
                返回;
            }
            如果(creditPlan ==2)
            {
                annualRate = 0.20; // 20%
                如果(状态==Y)
                {
                    晚=真;
                }                否则如果(状态==n个)
                {
                    晚= FALSE;
                }
                如果(晚==真)
                {
                    lateFee = 35.00;
                }
                如果(资产负债大于100)
                {
                    monthlyCharge =平衡*(annualRate *(1/12));
                }
                其他
                {
                    monthlyCharge = 0;
                }
                返回;
            }
            如果(creditPlan ==3)
            {
                annualRate = 0.15; // 15%
                lateFee = 0.00;                如果(平衡> 500)
                {
                    monthlyCharge =(平衡 - 500)*(annualRate *(1/12));
                }
                其他
                {
                    monthlyCharge = 0;
                }
                返回;
            }
            netBalance =平衡 - (lateFee + monthlyCharge);
            Console.WriteLine(姓名:\\ t \\ t \\ t {0} {1},名字,姓氏);
            Console.WriteLine(帐号:\\ t {0},accNumber);
            Console.WriteLine(信用飞机:\\ t \\ t {0},creditPlan);
            Console.WriteLine(账户后期:\\ t \\ t {0},晚);
            Console.WriteLine(余额:\\ t \\ t {0},平衡);
            Console.WriteLine(加收滞纳金:\\ t \\ t {0},lateFee);
            Console.WriteLine(利息费用:\\ t {0},monthlyCharge);
            Console.WriteLine(净余额:\\ t \\ t {0},netBalance);
            Console.WriteLine(年利率:\\ t \\ t {0},annualRate);
            Console.ReadKey();
        }
    }
}


解决方案

的编译器是不是足够聪明,知道你的如果块将被执行的至少一个。因此,它不会看到像 annualRate 变量将会不管分配。这里是你如何可以使编译器明白:

 如果(creditPlan ==0)
{
    // ...
}
否则如果(creditPlan ==1)
{
    // ...
}
否则如果(creditPlan ==2)
{
    // ...
}
其他
{
    // ...
}

编译器知道用if / else块,块之一是保证执行,因此,如果你在分配的所有块的变量,它不会给编译器错误。

顺便说一句,你也可以使用开关语句而不是如果 s到也许让你的code清洁。

I keep getting this error for annualRate, monthlyCharge, and lateFee..And for the death of me I cannot figure out why. Any help would be greatly appriciated!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab_5___Danny_Curro
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstName;
            string lastName;
            int accNumber;
            string creditPlan;
            double balance;
            string status;
            Boolean late = false;
            double lateFee;
            double monthlyCharge;
            double annualRate;
            double netBalance;



            Console.Write("Enter First Name: ");
            firstName = Console.ReadLine();

            Console.Write("Enter Last Name: ");
            lastName = Console.ReadLine();

            Console.Write("Enter Account Number: ");
            accNumber = Convert.ToInt32(Console.ReadLine());


            Console.Write("Enter Credit Card Plan Number[Blank Will Enter Plan 0]: ");
            creditPlan = Console.ReadLine();

            Console.Write("Enter Balance: ");
            balance = Convert.ToDouble(Console.ReadLine());

            Console.Write("Is This Account Late?: ");
            status = Console.ReadLine().Trim().ToLower();

            if (creditPlan == "0")
            {
                annualRate = 0.35;  //35%
                lateFee = 0.0;
                monthlyCharge = balance * (annualRate * (1 / 12));
                return;
            }

            if (creditPlan == "1")
            {
                annualRate = 0.30;  //30%
                if (status == "y")
                {
                    late = true;
                }

                else if (status == "n")
                {
                    late = false;
                }
                if (late == true)
                {
                    lateFee = 25.00;
                }
                monthlyCharge = balance * (annualRate * (1 / 12));
                return;
            }
            if (creditPlan == "2")
            {
                annualRate = 0.20;  //20%
                if (status == "y")
                {
                    late = true;
                }

                else if (status == "n")
                {
                    late = false;
                }
                if (late == true)
                {
                    lateFee = 35.00;
                }
                if (balance > 100)
                {
                    monthlyCharge = balance * (annualRate * (1 / 12));
                }
                else
                {
                    monthlyCharge = 0;
                }
                return;
            }
            if (creditPlan == "3")
            {
                annualRate = 0.15;  //15%
                lateFee = 0.00;

                if (balance > 500)
                {
                    monthlyCharge = (balance - 500) * (annualRate * (1 / 12));
                }
                else
                {
                    monthlyCharge = 0;
                }
                return;
            }
            netBalance = balance - (lateFee + monthlyCharge);


            Console.WriteLine("Name: \t\t\t {0}  {1}", firstName, lastName);
            Console.WriteLine("Account Number: \t{0}", accNumber);
            Console.WriteLine("Credit Plane: \t\t{0}",creditPlan);
            Console.WriteLine("Account Late: \t\t{0}", late);
            Console.WriteLine("Balance: \t\t{0}", balance);
            Console.WriteLine("Late Fee: \t\t{0}", lateFee);
            Console.WriteLine("Interest Charge: \t{0}", monthlyCharge);
            Console.WriteLine("Net Balance: \t\t{0}",netBalance);
            Console.WriteLine("Annual Rate: \t\t{0}", annualRate);
            Console.ReadKey();
        }
    }
}

解决方案

The compiler isn't smart enough to know that at least one of your if blocks will be executed. Therefore, it doesn't see that variables like annualRate will be assigned no matter what. Here's how you can make the compiler understand:

if (creditPlan == "0")
{
    // ...
}
else if (creditPlan == "1")
{
    // ...
}
else if (creditPlan == "2")
{
    // ...
}
else
{
    // ...
}

The compiler knows that with an if/else block, one of the blocks is guaranteed to be executed, and therefore if you're assigning the variable in all of the blocks, it won't give the compiler error.

By the way, you can also use a switch statement instead of ifs to maybe make your code cleaner.

这篇关于这是什么[使用未分配的局部变量&QUOT的;意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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