简体   繁体   English

我的计算器无法使用C#控制台应用程序

[英]My calculator won't work c# console application

Its probably a very easy solution but when it gives 3 error messages: 它可能是一个非常简单的解决方案,但是当它给出3条错误消息时:

The name 'iNum1' does not exist in the current context  
The name 'iNum2' does not exist in the current context  
The name 'soOper' does not exist in the current context 

When I remove my last line of code it works but without it I can't calculate it. 当我删除最后一行代码时,它可以工作,但是如果没有它,我将无法计算它。 I hope someone can help. 我希望有人能帮帮忙。 Here is the code. 这是代码。

//information
    Console.WriteLine("This is a calculator");

    //Let them fill in the first number
    Console.WriteLine("Please enter the first number");
    bool bNoNum1 = true;
    while (bNoNum1)
    {

        string sNum1 = Console.ReadLine();

        try
        {
            int iNum1 = int.Parse(sNum1);
            bNoNum1 = false;
        }

        catch (Exception)
        {
            Console.WriteLine("That's not a number");
        }

    }



    //Let them fill in (*, +, / of -)
    Console.WriteLine("Please enter +, +, - or :");

    bool bNoOperator = true;


    do
    {
        string sOper = Console.ReadLine();

        if (sOper == "x")
        {
            string soOper = "*";
            bNoOperator = false;
        }
        else if (sOper == ":")
        {
            string soOper = "/";
            bNoOperator = false;
        }
        else if (sOper == "+")
        {
            string soOper = "+";
            bNoOperator = false;
        }
        else if (sOper == "-")
        {
            string soOper = "-";
            bNoOperator = false;
        }
        else
        {
            Console.WriteLine("De operator " + sOper + " Is niet bekend. Kies uit +, -, x of :");
        }
    } while (bNoOperator);


    //Enter second number
    Console.WriteLine("Please enter the second number");

    bool bNoNum2 = true;
    while (bNoNum2)
    {
        string sNum2 = Console.ReadLine();

        try
        {
            int iNum2 = int.Parse(sNum2);
            bNoNum2 = false;
        }

        catch (Exception)
        {
            Console.WriteLine("That's not a number");
        }
    }

    //calculating

    int uitkomst = iNum1 + soOper + iNum2;

You need to declare those 3 variables as a global one's outside your context, put those variables above the line " like this, 您需要将这3个变量声明为上下文之外的全局变量,将这些变量放在“

Console.WriteLine("This is a calculator");
"
int iNum1;
int iNum2;
string sOper = "";

You declare iNum1 and iNum2 at the wrong place - inside some inner brackets. 您在错误的位置(在某些内部括号内)声明了iNum1和iNum2。 They are not known at the scope where the last line lives. 在最后一行所在的范围内,它们是未知的。 Declare those variables at a different level. 在不同级别上声明这些变量。

In any case when you will do that you will have another issue: soOper is a string. 无论如何,您都会遇到另一个问题:soOper是一个字符串。 You are adding an int with a string and with another int. 您正在使用一个字符串和另一个int添加一个int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM