且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

全局变量和局部变量有问题 - c#

更新时间:2022-06-07 05:44:26

字符串文字需要用引号括起来 - plate,而不是 plate



变量需要明确分配才能使用。编译器不够聪明,以至于在 isValidOption 设置为 true $时,将始终分配价格和描述。 C $ C>。在以前的 if 块中写入成功消息会更容易。



你继续即使您找到了有效的项目,也会遍历项目。一旦找到该项目,你应该中断



你正在写无效项目消息太快 - 例如,如果用户输入杯子会怎样?



isValidOption 应该是 GetDetails 方法中的局部变量。您只需要知道在循环结束后是否写入无效项消息。



伪代码:

String literals need to be enclosed in quotes - "plate", not plate.

Variables need to be definitely assigned before they can be used. The compiler isn't smart enough to work out that the price and description will always be assigned by the time that isValidOption is set to true. It would be easier to write the success message inside the previous if block instead.

You're continuing to loop over the items even after you've found a valid item. You should break out of the loop as soon as the item is found.

You're writing the Invalid item message too soon - what happens if the user enters "cup", for example?

isValidOption should really be a local variable within the GetDetails methods. You only need it to know whether or not to write the Invalid item message after the loop has finished.

Pseudo-code:
bool isValidItem = false;

For each index in the range:
{
    If the item at this index matches the entered item:
    {
        Write the "found it" message
        Set isValidItem to true
        Break out of the for loop
    }
}

If isValidItem is false:
{
    Write the "invalid item" message
}


Ok * facepalm *我应该知道字符串数组中的引号。非常感谢你的回复!



所以我尝试了你对我的方法所说的话,它给了我一个不能在这个范围内声明,因为它会给'isValidItem'赋予不同的含义错误。当我把它保存为isValidOption = true时,错误就消失了;但我不认为isValidOption变量被改为true,因为我的输出出错了。当我回去并将代码行更改为bool isValidItem = true;时(试图将其改为true)它会吐出不同的意义错误。



这是我当前的代码,就像我说的我不知道如何将布尔变量更改为true,或者为什么这不起作用。 />


Ok *facepalm* I should have known that about the quotes in the string array. Thank you so much for the reply!

So I tried what you said about my method and it was giving me a "cannot be declared in this scope because it would give a different meaning to 'isValidItem'" error. The error went away when I kept it as isValidOption = true; but I don't think the isValidOption variable was getting changed to true because my output came out wrong. When I went back and changed the line of code to "bool isValidItem = true;" (trying to change it to true) it would spit out that different meaning error.

This is my current code, Like I said I'm not sure how else to change the boolean variable to true, or why this isn't working.

private static void GetDetails(string word)
        {
            bool isValidOption = false;
            string desc1;
            int item1;
            double price1;
            for (int x = 0; x < Global.description.Length; ++x)

            {

                if (word == Global.description[x])
                {

                    desc1 = Global.description[x];
                    price1 = Global.price[x];
                    item1 = Global.itemNumber[x];
                    bool isValidOption = true;
                    Console.WriteLine("You selected {0} which is item # {1} and costs {2}.", desc1, item1, price1);
                    break;
                }
            }
                    if (isValidOption == false)
                    {
                        Console.WriteLine("Invalid item # String");

                    }
        }





这是我所在的2个实例中的当前错误试图将isValidOption更改为true并且它不起作用:



错误1无法在此范围内声明名为'isValidOption'的局部变量,因为它会给出不同的意思是'isValidOption',它已经在'父或当前'范围内用于表示别的东西



也许我无法改变它因为它在里面for和if语句?我尝试将公共放在布尔声明的前面。



就像我说我的输出错了。



(错误的输出我的意思是无效的项目#总是显示,有时多次显示)



再次感谢!



This is the current error from the 2 instances where I'm trying to change the isValidOption to true and it's not working:

"Error 1 A local variable named 'isValidOption' cannot be declared in this scope because it would give a different meaning to 'isValidOption', which is already used in a 'parent or current' scope to denote something else"

Maybe I can't change it because it's inside a for and an if statement? I tried putting "public" in front of the boolean declaration too though.

Like I said my output was wrong.

(By wrong output I mean the invalid item # was always showing and sometimes multiple times)

Thanks again!