2016-08-15 4 views
-1

私はC#の初心者です。ここでは基本的なループ計算機(以下のコード)を書いています。ループブレイクとして定義されたプログラムを「終了」すると、31行目に「System.FormatExceptionがスローされました(未知の文字)」というエラーが発生します。誰かがこの意味を説明し、そのようなエラーを避けるためにコードを編集する方法を教えてください。ありがとうございました。このエラーは何を意味しますか?「System.FormatExceptionがスローされました。未知の文字」?

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) 
     { 
      // Greeting. 

      Console.WriteLine ("Welcome to calculator."); 
      Console.WriteLine ("Enter 'Exit' to stop."); 

      // Persistent loop with an exit option. 

      while (true) 
      { 

      // Get three input values from user, separated by spaces. 

      Console.Write ("Input two values with an operation, with spaces between."); 

      // Get values from user. 

      string text = Console.ReadLine(); 
      string[] parts = text.Split (' '); 
      float value1 = Convert.ToSingle (parts [0]); 
      string operation = parts [1]; 
      float value2 = Convert.ToSingle (parts [2]); 

      // Loop exit option. 

      if(text == "exit" || text == "Exit") 
       break; 

      // Establish the variable to store the result, 
      // initialized to zero, to be updated by the computation. 

      float result = 0; 

      // Switch between operations depending on the symbol input. 

       switch (operation) 
       { 
        case "+": 
         result = value1 + value2; 
         break; 
        case "-": 
         result = value1 - value2; 
         break; 
        case "*": 
         result = value1 * value2; 
         break; 
        case "/": 
         result = value1/value2; 
         break; 
        case "%": 
         result = value1 % value2; 
         break; 
        case "^": 
         result = (float)Math.Pow (value1, value2); 
         break; 
        default: 
         Console.WriteLine ("ERROR"); 
         break; 
       } 

       // Print out the result. 

       Console.WriteLine (value1 + " " + operation + " " + value2 + " = " + result); 
      } 
     } 
    } 
} 
+0

なぜあなたはvar input = Console.ReadLine()で始まっていないのでしょうか?そして、入力の結果をあなたの電卓の "終了"か式で確認してください。あなたは最初のプロンプトは、 "間にスペースを入れた操作で2つの値を入力するか、終了するには 'exit'と入力する必要があります。 –

+0

これは、whileループの最後に、exitの代わりに 'exit'を要求する必要があることを意味します。 – kurakura88

+0

簡単な解決策 - 意味があります、ありがとう! – jarombra

答えて

1

このような?

... 
Console.WriteLine ("Welcome to calculator."); 
Console.WriteLine ("Enter 'Exit' to stop."); 

while(true) { 
    Console.Write ("Input two values with an operation, with spaces between."); 
    string text = Console.ReadLine(); 
    if(text == "exit" || text == "Exit") 
     break; 
    string[] parts = text.Split (' '); 
    ... 
+0

私は、「Enterを押して終了する」という行が「2つの値を入力して、間にスペースを入れて」の最後に追加する方が適切だと思います。このようにして、ユーザは計算のたびに終了するために何をしなければならないかを知ることになる。 – ZerosAndOnes

+0

ありがとうM. Raees、これは理にかなっています!私はちょうど新しいものを何もしなくても少しだけ私の操作を並べ替える必要があると思った。 – jarombra

+0

不必要に定義された新しい変数( 'exit')ではなく、単に 'text'を参照するようにif文を変更しました。しかし、ここでは、プログラムを終了すると、 "System.FormatExceptionがスローされた不明なchar"エラーが発生する、フォローアップの質問です。それについては何ですか?私はそれに対処するために私の質問を更新するつもりです。 – jarombra

関連する問題