2017-04-25 138 views
0

我一直在尋找已經回答了這個問題的解決方案,但他們沒有一個似乎與我的代碼一起工作。我只是剛剛開始在C#中編寫代碼,而且我很困惑我的代碼是如何工作的。C#,將字符串轉換爲double的最佳方式是什麼?

using System; 

namespace dt 
{ 
    class Averager 
    { 
    static void Main() 
    { 
     var total = 0.0; 
     int runningNumbers = 0; 

     while(true) 
     { 
     Console.Write("Enter a number or type \"done\" to see the average: "); 

     if(Console.ReadLine().ToLower() == "done") 
     { 
      var average = (total/runningNumbers); 
      Console.Write("Average: " + average); 
      break; 
     } 
     else 
     { 
      var tempNew = Double.Parse(Console.ReadLine()); 
      total += tempNew; 
      runningNumbers += 1; 
      continue; 
     } 

     } 
    } 
    } 
} 

在我上面的代碼中,我試圖將輸入的字符串轉換爲double。當我運行程序時,它循環一次,程序凍結。如果我在控制檯輸入,它給了我這個錯誤:

Unhandled Exception:

System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble (System.String value, System.Globalization.NumberStyles options, System.Globaliz
at System.Double.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.Number
at System.Double.Parse (System.String s, System.IFormatProvider provider) [0x0000c] in at dturato.Averager.Main() [0x00058] in <249eeff07c4a4744a6b024a0c9b6c23b>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble (System.String value, System.Globalization.NumberStyles options, System.Globaliz
at System.Double.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.Number
at System.Double.Parse (System.String s, System.IFormatProvider provider) [0x0000c] in at d.Averager.Main() [0x00058] in <249eeff07c4a4744a6b024a0c9b6c23b>:0**

這一點,從我目前爲止的研究,意味着字符串沒有被轉換爲雙出於某種原因。如果任何人都可以提供幫助或提供任何好的解決方案,謝謝。

+0

您是否試過單步執行代碼以查看拋出異常時傳入'tempNew'的內容? – greenjaed

+0

請仔細閱讀[MCVE]關於發佈代碼的指導(帖子中的帖子顯然不是最小的)。還要確保提供你認爲「更好」的東西(或者當你需要「工作解決方案」時不要求「最好」)。 –

回答

3

您打電話給ReadLine兩次!第一次是在提示符後。第二個是你的if語句的else條款。第二個可能爲null,並在嘗試轉換爲double時導致錯誤。您需要將輸入的值存儲到變量中:

using System; 

namespace dt 
{ 
    class Averager 
    { 
    static void Main() 
    { 
     var total = 0.0; 
     int runningNumbers = 0; 

     while(true) 
     { 
     Console.Write("Enter a number or type \"done\" to see the average: "); 
     string input = Console.ReadLine(); 

     if(input.ToLower() == "done") 
     { 
      var average = (total/runningNumbers); 
      Console.Write("Average: " + average); 
      break; 
     } 
     else 
     { 
      var tempNew = Double.Parse(input); 
      total += tempNew; 
      runningNumbers += 1; 
      continue; 
     } 

     } 
    } 
    } 
} 
+0

這工作!感謝您的幫助,不知道我怎麼沒有意識到,從一開始 –

+0

這個答案甚至沒有試圖回答真正的問題 - 「將字符串轉換爲雙精度的最佳方式是什麼?」 ...也許問題需要編輯 –

3

該錯誤告訴您具體的問題。

Input string was not in a correct format.

您需要確保解析方法中的輸入有效。在大多數情況下,您會發現Parse方法的TryParse變體。在這種情況下,double.TryParse就是這樣做的。

using System; 

public class Example 
{ 
    public static void Main() 
    { 
     string value; 
     double number; 

     value = Double.MinValue.ToString(); 
     if (Double.TryParse(value, out number)) 
     Console.WriteLine(number); 
     else 
     Console.WriteLine("{0} is outside the range of a Double.", 
          value); 

     value = Double.MaxValue.ToString(); 
     if (Double.TryParse(value, out number)) 
     Console.WriteLine(number); 
     else 
     Console.WriteLine("{0} is outside the range of a Double.", 
          value); 
    } 
} 
// The example displays the following output: 
// -1.79769313486232E+308 is outside the range of the Double type. 
// 1.79769313486232E+308 is outside the range of the Double type. 

要引用回到你的代碼,你最初的問題是,你正在閱讀的輸入多次:試試這個

using System; 

namespace dt 
{ 
    class Averager 
    { 
    static void Main() 
    { 
     var total = 0.0; 
     int runningNumbers = 0; 

     while (true) 
     { 
      // Ask for user for a new input 
      Console.Write("Enter a number or type \"done\" to see the average: "); 
      var line = Console.ReadLine(); 

      // Try to parse the input 
      double value; 
      if(double.TryParse(line, out value)) { 
       total += value; 
       runningNumbers += 1; 
       continue; 
      } else if(line.ToLower() == "done") 
      { 
       var average = (total/runningNumbers); 
       Console.Write("Average: " + average); 
       break; 
      } else { 
       Console.Write("Invalid input. Please try again."); 
       continue; 
      } 

     } 
    } 
    } 
} 
1

我只是把這些東西它變成一個雙馬上蝙蝠 - 使用「Convert.toDouble(yourValue)」,它表示你已經在做檢查,看看你是否會得到一個數值或一個字符串,那麼爲什麼不跳過這個var並且讓它立刻變成雙重的?

using System;

namespace dt{ 
    class Averager 
     static void Main(){ 
    var total = 0.0; 
    int runningNumbers = 0; 

    while(true) 
    { 
    Console.Write("Enter a number or type \"done\" to see the average: "); 
    var test = Console.Readline(); 
    if(test.ToLower() == "done") 
    { 
     var average = (total/runningNumbers); 
     Console.Write("Average: " + average); 
     break; 
    } 
    else 
    { 
     double tempNew = Convert.toDouble(test); 
     total += tempNew; 
     runningNumbers += 1; 
     continue; 
    } 

    } 
    } 
} 
} 
相關問題