C#警告:“已分配变量,但从未使用过其值” [英] C# warning: "The variable is assigned but its value is never used"

查看:540
本文介绍了C#警告:“已分配变量,但从未使用过其值”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学C#的人,刚刚开始编写我的第一个程序。我目前在while循环之一中遇到障碍。通常,我发现循环之外我的变量按预期运行,但是由于某种原因,Visual Studio发出警告说变量'itemPrice'已分配,但从未使用过它的值。

I am a first-timer learning C# and I just started writing my first program. I am currently running into a snag in one of my while loops. Normally I find that outside the loop my variables work like they should, but for some reason Visual Studio is giving me a warning saying that "The variable 'itemPrice' is assigned but its value is never used."

我如何获得该警告以消除并避免任何不良行为?

How I can I get that warning to go away and avoid any kind of bad practices?

using System;

class Program
{
    public static void Main()
    {

        // "whoa" <-- string literal
        // 43 <-- int literal
        // 43.34 <-- double literal
        // false <-- bool literal
        // 43.4f <-- float literal
        // 43f <-- float literal
        // 43m <-- decimal literal

        bool hasEnteredMoney = false;
        decimal money = 0;
        int menuSelection;
        decimal itemPrice;
        bool isValidItem;
        string itemName;

        while (!hasEnteredMoney)
        {
            Console.Write("Please enter how much money you have: ");
            hasEnteredMoney = decimal.TryParse(Console.ReadLine(), out money);

            if (!hasEnteredMoney)
            {
                Console.WriteLine("Please enter a valid decimal value.");
            }
        }

        while (money >= 3)
        {
            Console.WriteLine("You have $" + money + " left.");
            Console.WriteLine("Please enter your selection:");
            Console.WriteLine("[0] - Poke Ball - $200");
            Console.WriteLine("[1] - Great Ball - $700");
            Console.WriteLine("[2] - Ultra Ball - $1200");
            Console.WriteLine("[3] - Potion - $300");
            Console.WriteLine("[4] - Antidote - $100");
            Console.WriteLine("[5] - Clear Mail - $50");
            Console.WriteLine(": ");

            if (int.TryParse(Console.ReadLine(), out menuSelection))
            {

                isValidItem = false;

                if (menuSelection == 0)
                {
                    itemPrice = 200;
                    itemName = "Poke Ball";
                    isValidItem = true;
                }

                if (menuSelection == 1)
                {
                    itemPrice = 700;
                    itemName = "Great Ball";
                    isValidItem = true;
                }

                if (menuSelection == 2)
                {
                    itemPrice = 1200;
                    itemName = "Ultra Ball";
                    isValidItem = true;
                }

                if (menuSelection == 3)
                {
                    itemPrice = 300;
                    itemName = "Potion";
                    isValidItem = true;
                }

                if (menuSelection == 4)
                {
                    itemPrice = 100;
                    itemName = "Antidote";
                    isValidItem = true;
                }

                if (menuSelection == 5)
                {
                    itemPrice = 50;
                    itemName = "Clear Mail";
                    isValidItem = true;
                }
            }
            else
            {
                Console.WriteLine("Invalid input.");
            }

        }

        Console.WriteLine("... you ran out of money :(");

        Console.ReadKey();

    }
}


推荐答案

正在设置(分配给)变量 itemPrice,isValidItem,itemName ,但是稍后在程序中这些变量将不会用于其他目的

The variables itemPrice, isValidItem, itemName are being set (being assigned to), but the variables are not being used for other purposes later in the program.

编译器警告您,尽管您设置了 itemPrice,isValidItem itemName ,设置这些变量不会在程序中执行任何操作-EG在执行程序时不会对程序的运行产生任何影响。

The compiler is warning you that although you are setting itemPrice, isValidItem, and itemName, setting these variables is not doing anything in the program - E.G. when the program is executed, the variables will not have any effect on the operation of the program.

这篇关于C#警告:“已分配变量,但从未使用过其值”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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