2015-09-20 20 views
0

它可能是一個非常簡單的解決方案,但時,它提供3個錯誤信息:我的計算器是行不通的C#控制檯應用程序

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 

當我刪除我的代碼最後一行它的工作原理,但沒有它,我不能計算它。我希望有人能幫幫忙。這是代碼。

//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; 
+1

相關:http://stackoverflow.com/questions/2693138/variable-scope-in​​-statement-blocks – rene

+1

你不是第一個遇到這個問題:http://stackoverflow.com/search? q = does + not + exist + in + the current + context +%5Bc%23%5D – rene

+1

[C#Math calculator](http://stackoverflow.com/questions/2859111/c-sharp-math-calculator ) – Luke

回答

2

您需要聲明的3個變量作爲一個全球性的你的背景之外,放線「這樣的上述這些變量,

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

你在錯誤的地點申報iNum1和iNum2 - 內一些內部的括號,它們在最後一行所在的範圍內是未知的,在不同的級別聲明這些變量

在任何情況下,當你這樣做時,你將會遇到另一個問題:soOper是一個字符串。用一個字符串和另一個int添加一個int。

相關問題