2014-09-26 28 views
0
using System; 
class SumDoubles 
{ 
    static void Main() 
    { 
     //Declare variables 
     double DblSumTotal = 0; 
     double LIMIT = 0; 


     //Ask user to input 5 numbers to be added 
     Console.Clear(); 
     Console.WriteLine("Enter 5 numbers to be added together."); 
     do 
     { 
      DblSumTotal = DblSumTotal + (Convert.ToDouble(Console.ReadLine())); 
      LIMIT = LIMIT + 1; 
     } while (LIMIT < 6); 

     //Output total 
     Console.WriteLine("The total sum of the 5 numbers is " + DblSumTotal); 
     Console.ReadLine(); 
    } 
} 

這是我到目前爲止的代碼。它會編譯但試圖運行時收到錯誤:詢問用戶5個號碼,然後加在一起使用一個循環

未處理的異常:輸入字符串的格式不正確。在System.Number.ParseDouble(字符串值,選擇的NumberStyles,NumberFormat的信息numfmt)在System.Double.Parse(字符串s的IFormatProvider提供商)在SumDoubles.Main()」

我很新的編碼和我似乎無法換我的頭周圍迴路。請幫助!

+2

什麼是輸入? – 2014-09-26 14:37:54

+0

輸入將是數字。我通常嘗試將5個1加在一起。輸出應該是「5個數字的總和是5」,但我收到一個錯誤 – LeFemur 2014-09-26 14:39:24

+0

我在哪裏轉換錯了? – LeFemur 2014-09-26 14:39:43

回答

0

Sasse和Tejas Sharma都有一個觀點。然後完成代碼可能是這個樣子:

using System; 
class SumDoubles 
{ 
    static void Main() 
    { 
     //Declare variables 
     double DblSumTotal = 0; 
     double LIMIT = 0; 


     //Ask user to input 5 numbers to be added 
     Console.Clear(); 
     Console.WriteLine("Enter 5 numbers to be added together."); 
     do 
     { 
      double d; 
      if (!double.TryParse(Console.ReadLine(), out d)) { 
       Console.WriteLine("Format error!!!"); 
      } else { 
       DblSumTotal = DblSumTotal + d; 
       LIMIT = LIMIT + 1; 
      } 

     } while (LIMIT < 5); 

     //Output total 
     Console.WriteLine("The total sum of the 5 numbers is " + DblSumTotal); 
     Console.ReadLine(); 
    } 
} 

` 另外,如果你和我一樣是使用非英語語言環境(我有一個捷克的系統),然後可以分析翻一番串看起來不同的(即捷克語「1,2」與英語「1.2」)

2

Convert.ToDouble拋出時,它得到錯誤的輸入如一個空字符串,你所看到的例外。你可能要麼需要使用try catch塊來處理異常或你可以選擇使用double.TryParse(),這樣不會導致輸入錯誤。

double output; 
bool isValid = double.TryParse("10", out output); // isValid is true and output contains 10 
isValid = double.TryParse("lsdf", out output); // isValid is false 

我還注意到,您在這段代碼中有一個「關1」錯誤

Console.WriteLine("Enter 5 numbers to be added together."); 
do 
{ 
    DblSumTotal = DblSumTotal + (Convert.ToDouble(Console.ReadLine())); 
    LIMIT = LIMIT + 1; 
} while (LIMIT < 6); 

你的循環有6次迭代(LIMIT = 0,1,2,3,4,5),你期待5個數字。您可能正在按一個額外的「輸入」,導致空字符串""傳遞到Convert.ToDouble,從而導致例外。將while (limit < 6)更改爲while (limit < 5)

1

你的程序工作得很好,除了它讀取六個數字而不是五個。

我認爲你的問題與xeraphim所說的有關,你試圖在同一時間輸入所有的數字。

如果您輸入「4」ENTER「5」ENTER「10」ENTER等等,它將工作得很好。 =)

PS:請記住,「。」之間有區別。和「,」。

+0

這是我在做什麼我輸入1,然後輸入5次,並收到錯誤 – LeFemur 2014-09-26 14:44:27

+0

正如我所說,它期望你做6次就像現在一樣。所以如果你最後一次只是按下「ENTER」鍵,那麼它會把你的空字符串並嘗試轉換它,這將失敗,並拋出異常。 – Sasse 2014-09-26 14:45:40

+0

@LeFemur'type 1並輸入'5次,或輸入1,然後'輸入5次'?因爲空的語句會創建異常。你可以設置一個斷點並對其進行調試。 – 2014-09-26 14:49:11

-2

我認爲這可以通過使用for循環來簡化。

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

     namespace ConsoleApplication2 
     { 
      class Program 
      { 
       static void Main(string[] args) 
       { 
        int read; 
        int sum = 0; 
        Console.WriteLine("Enter 5 numbers: "); 
        for (int i = 0; i < 5; i++) 
        { 
         read = int.Parse(Console.ReadLine()); 
         sum = sum + read; 
        } 

        Console.WriteLine("The total sum of the 5 numbers are " + sum); 
       } 
      } 
     } 

或者,如果你仍然想使用while循環,就像一樣容易。

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

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int read; 
      int sum = 0; 
      int i = 0; 
      Console.WriteLine("Enter 5 numbers: "); 
      while(i<5) 
      { 
       read = int.Parse(Console.ReadLine()); 
       sum = sum + read; 
       i++; 
      } 

      Console.WriteLine("The total sum of the 5 numbers are " + sum); 
     } 
    } 
} 
+0

OP的問題是由於格式不正確而導致失敗,簡化循環有幫助嗎? – Isantipov 2014-09-26 15:03:40

+0

不,這很好,這也是非常有幫助的。你們都可能意識到這也是爲了一個學校任務 – LeFemur 2014-09-26 15:08:09

相關問題