2014-11-01 15 views
-1

如果你看看我的代碼的底部,我使用變量'值',當我得到'使用未分配的變量'錯誤。有沒有人知道任何祕密的嚮導技巧或代碼的不同佈局,使我能夠在if語句中使用相同的變量並在單獨的while循環(不同的塊)中使用它?由於使用未分配的變量錯誤,我想不出一個解決方法

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

namespace Calculator 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Calculator V1.00"); 
      Console.WriteLine("----------------"); 
      Console.WriteLine(""); 
      Console.WriteLine("Enter a number:"); 

      bool bool1 = false; 
      double value; 
      while (bool1 == false) 
      { 
       try 
       { 
         value = Convert.ToDouble(Console.ReadLine()); 
         bool1 = true; 
       } 
       catch (FormatException) 
       { 
        Console.WriteLine("Enter a number"); 
        bool1 = false; 
       } 
      } 
      bool bool2 = false; 
      Console.WriteLine("----------------"); 
      Console.WriteLine("----------------"); 
      Console.WriteLine("Now enter an operator such as '+' or '*' "); 

      char op; 
      double value2; 
      while (bool2 == false) 
      { 
       try 
       { 
        op = Convert.ToChar(Console.ReadLine()); 
        bool2 = true; 

        Console.WriteLine(""); 
        Console.WriteLine("Now input your second number"); 
        Console.WriteLine("----------------------------"); 
        value2 = Convert.ToDouble(Console.ReadLine()); 
        if (op == Convert.ToChar("+")) 
        { 
         double result = value + value2; 
          Console.WriteLine(result); 
        } 

       } 
       catch (FormatException) 
       { 
        Console.WriteLine("Now enter an operator such as '+' or '*' "); 
        bool2 = false; 
       } 
      } 
     } 
    } 
} 
+1

更好的是'而(BOOL1!)',更是將使用一個更具描述名稱,如「完成」或「完成」等。命名變量'bool1,bool2'被認爲是不好的風格。 – JensG 2014-11-01 15:56:09

回答

1

給您的變量的默認值:

double value = 0; 

它不知道,如果你永遠得到成while迴路設置value變量。

while (bool1 == false) 

如果bool1在你的代碼被初始化爲true什麼?該循環將永遠不會執行,並且value在以後的方法中不會有值。另外,FWIW,您需要重命名所有這些變量(值,值2,布爾1,布爾2等)以使其更具有意義,以便您可以在以後快速告訴它們的含義。

+0

「*如果bool1在您的代碼中初始化爲true,會怎麼樣?*」 - 實際情況並非如此。 'bool1'被正確初始化。 – JensG 2014-11-01 15:58:10

+0

@JensG我知道這並不是當前的情況,但是如果James改變它,所以它在未來某個日期初始化爲'bool bool1 = true;',那麼while循環將永遠不會執行,並且'value'不會得到一個值。這就是爲什麼他得到警告......它認識到_potential_它不會被分配一個值。 – 2014-11-01 16:00:20

0

把你的輸入請求在一個單獨的方法:

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

namespace Calculator 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Calculator V1.00"); 
      Console.WriteLine("----------------"); 
      Console.WriteLine(""); 
      Console.WriteLine("Enter a number:"); 

      double value = GetValue(); 

      bool bool2 = false; 
      Console.WriteLine("----------------"); 
      Console.WriteLine("----------------"); 
      Console.WriteLine("Now enter an operator such as '+' or '*' "); 

      while (bool2 == false) 
      { 
       try 
       { 
        char op = Convert.ToChar(Console.ReadLine()); 
        bool2 = true; 

        Console.WriteLine(""); 
        Console.WriteLine("Now input your second number"); 
        Console.WriteLine("----------------------------"); 

        double value2 = GetValue(); 

        if (op == Convert.ToChar("+")) 
        { 
         double result = value + value2; 
          Console.WriteLine(result); 
        } 

       } 
       catch (FormatException) 
       { 
        Console.WriteLine("Now enter an operator such as '+' or '*' "); 
        bool2 = false; 
       } 
      } 
     } 

     private static double GetValue() 
     { 
      bool bool1 = false; 

      while (bool1 == false) 
      { 
       try 
       { 
         return Convert.ToDouble(Console.ReadLine()); 
       } 
       catch (FormatException) 
       { 
        Console.WriteLine("Enter a number"); 
        bool1 = false; 
       } 
      } 

      throw new InvalidOperationException(); 
     } 
    } 
} 

由於一個副作用,你可以重新使用該方法獲得value2與相同的錯誤處理。

1

你可能想只考慮像while(true)這樣的無限循環。只要在收到有效輸入時使用break語句即可解決問題。

這樣,你可以做掉你BOOL1,BOOL2,BOOL3

+0

這正是我想要建議的。您需要編寫的代碼越少,錯誤發生的機會就越小。 – hvd 2014-11-01 16:13:42

+0

「*您需要編寫的代碼越少,錯誤出現的機會就越少*」 - 這只是中途的一個答案。鑑於解決問題的任務是解決10個班輪中充滿不可維護,高度「優化」的結構,這是一些「天才」寫的很長的遙不可及,或者在一個看起來完全無聊簡單的50班裏,我拿50班班輪離開你用「天才」代碼。去過也做過。 – JensG 2014-11-01 16:28:32

+1

@JensG我從來沒有暗示過,也從來不會暗示代碼簡潔是可維護性的唯一標準,但它是* a *標準。我認爲宣佈這一建議不可維護,沒有相關的區別,但請詳細說明,如果你這樣做。 – hvd 2014-11-01 18:00:56

0

通過這兩種輸入(雙和焦炭)的分成方法,你會得到一些好處。不僅您的實際問題將得到解決,而且代碼更加清晰,因爲我們將不同級別的細節或抽象級別分開。最後,我們可以完全拋出bool變量。我離開這個樣板出來爲簡潔:

static double EnterNumber(string descr) 
{ 
    while (true) 
    { 
     try 
     { 
      Console.WriteLine(descr); 
      var value = Convert.ToDouble(Console.ReadLine()); 
      return value; 
     } 
     catch (FormatException) 
     { 
      // just try again 
     } 
    } 
} 


static char EnterOperator(string descr) 
{ 
    while (true) 
    { 
     try 
     { 
      Console.WriteLine(descr); 
      var op = Convert.ToChar(Console.ReadLine()); 
      if(("+-*/").IndexOf(op) >= 0) 
       return op; 

     } 
     catch (FormatException) 
     { 
      // just try again 
     } 
    } 
} 

static void Calculate(double val1, char op, double val2) 
{ 
    switch (op) 
    { 
     case '+': 
      Console.WriteLine(val1 + val2); 
      break; 
     case '-': 
      Console.WriteLine(val1 - val2); 
      break; 
     case '*': 
      Console.WriteLine(val1 * val2); 
      break; 
     case '/': 
      Console.WriteLine(val1/val2); 
      break; 
     default: 
      Console.WriteLine("Unhandled operator "+op); 
      break; 
    } 
} 

這使得我們與此內容的主程序:

static void Main(string[] args) 
{ 
    Console.WriteLine("Calculator V1.00"); 
    Console.WriteLine("----------------"); 
    Console.WriteLine(""); 

    double val1 = EnterNumber("Enter first number:"); 
    char op = EnterOperator("Now enter an operator such as '+' or '*':"); 
    double val2 = EnterNumber("Now input your second number:"); 

    Calculate(val1, op, val2); 
} 
相關問題