2016-09-26 13 views
-1

問題のあるサンプルのコードを書きました。コード」Visual Studio 2015コミュニティC#電卓のコードエラー

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

namespace SEC3_LEC19 { 
    class Program { 
    static void Main(string[] args) { 

     int x, y; 
     string num1, num2, choice = "yes"; 
     char op; 
     bool again = true; 

     while (again) { 

     Console.WriteLine("Enter two integers"); 
     // First Number 
     Console.Write("Enter num1: "); 
     num1 = Console.ReadLine(); 

     // Second Number 
     Console.Write("Enter num2: "); 
     num2 = Console.ReadLine(); 

     // The Operator 
     Console.Write(
      "Enter the operation [+ - * /]: "); 
     op = (char)Console.Read(); 

     if (int.TryParse(num1, out x)) { 
      ; 
     } else { 
      Console.WriteLine(
      num1 + " is NaN val set to 0"); 
     } 

     if (int.TryParse(num2, out y)) { 
      ; 
     } else { 
      Console.WriteLine(
      num2 + " is NaN val set to 0"); 
     } 

     switch (op) { 
      case '+': 
      Console.WriteLine(
       x + " + " + y + " = " + (x + y)); 
      break; 
      case '-': 
      Console.WriteLine(
       x + " - " + y + " = " + (x - y)); 
      break; 
      case '*': 
      Console.WriteLine(
       x + " * " + y + " = " + (x * y)); 
      break; 
      case '/': 
      if (y == 0) { 
       Console.WriteLine(
       "Division by zero not allowed!"); 
      } else { 
       Console.WriteLine(
       x + "/" + y + " = " + (x - y)); 
      } 
      break; 
      default: 
      Console.WriteLine(
       "Operator Unrecognized"); 
      break; 
     } 

     // Offer user to try again 
     Console.Write("Go again? [yes/no]: "); 

     // Read user input [NOT WORKING] 
     choice = Console.ReadLine(); 

     switch (choice.ToLower()) { 
      case "yes": 
      again = true; 
      break; 
      case "no": 
      again = false; 
      break; 
      default: 
      Console.WriteLine(
       "Unrecognized choice!"); 
      break; 
     } 
     } 

     // ********************************************** 
     Console.WriteLine("Press any key to continue.."); 
     Console.ReadKey(); 
    } 
    } 
} 

コードは、コンソールを介して、whileループを使用して2つの数値を入力するようにユーザに依頼するために、それが実行し、計算結果を表示した後、オペレータ。彼らはユーザーに再度試してみるかどうか聞いてみる。これはchoice = Console.ReadLine()ステートメントを使用します。答えに基づいて、コードはwhileループを続行するか、ループから抜け出すことです。残念ながら、コンパイラはその選択部分をスキップし、switch文に進むだけです。コードの上部にも同様の記述があり、うまく動作します。

どのような考えにも感謝します。

+2

は、あなたのコードにリンクしないでください。質問に関連するすべてのコードを含めます。 –

+0

@Quantic彼はユーザーの入力を待つことになっています。 –

+0

省略部分を見せていただけますか?あなたはどのように2つの数字の値を読んでいますか? – Steve

答えて

0

これは、34行目でConsole.Read()を使用しているためです.1文字読み込み、Enterキーを押すと戻りキーも読み込まれます。したがって、バッファにはすでにキーがあり、ReadLine()をスキップして改行として読み込みます。

+0

うーん、私はそれについて考えなかった。ありがとうございました:) –

0

opを文字列に変更し、Read()ではなくReadLine()を変更すると、入力ストリームに保留中/非表示文字が存在しないことが確認されます。その後、op場合は、スイッチのチェックは、私はまた、あなたのインデントを固定

文字列"+""-""*" ...などといないのchar '+''-' ...などです。読んで理解した方がよいように、すべてのあなたの将来の実装では、このスタイルに従ってください:

をこれは動作するはずです:

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

namespace SEC3_LEC19 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      int x, y; 
      string num1, num2, choice = "yes"; 
      string op; // changed to string 
      bool again = true; 

      while (again) 
      { 

       Console.WriteLine("Enter two integers"); 

       Console.Write("Enter num1: "); 
       num1 = Console.ReadLine(); 

       Console.Write("Enter num2: "); 
       num2 = Console.ReadLine(); 

       Console.Write("Enter the operation [+ - * /]: "); 
       op = Console.ReadLine(); // Read line instead of Read char 

       if (!int.TryParse(num1, out x)) 
        Console.WriteLine(num1 + " is NaN val set to 0"); 

       if (!int.TryParse(num2, out y)) 
        Console.WriteLine(num2 + " is NaN val set to 0"); 

       switch (op) 
       { 
        // Changed cases 
        case "+": Console.WriteLine(x + " + " + y + " = " + (x + y)); break; 
        case "-": Console.WriteLine(x + " - " + y + " = " + (x - y)); break; 
        case "*": Console.WriteLine(x + " * " + y + " = " + (x * y)); break; 
        case "/": 
         if (y == 0) 
          Console.WriteLine("Division by zero not allowed!"); 
         else 
          Console.WriteLine(x + "/" + y + " = " + (x - y)); 
         break; 

        default: 
         Console.WriteLine("Operator Unrecognized"); break; 
       } 

       Console.Write("Go again? [yes/no]: "); 

       choice = Console.ReadLine(); 

       switch (choice.ToLower()) 
       { 
        case "yes": again = true; break; 
        case "no": again = false; break; 
        default: Console.WriteLine("Unrecognized choice!"); break; 
       } 
      } 

      Console.WriteLine("Press any key to continue.."); 
      Console.ReadKey(); 
     } 
    } 
} 
+0

Awsome! 私はかなり小さいモニタを使用していますので、私のコードのフォーマットは水平よりも垂直に広がっていなければなりません。しかし、私はpastebinでstackoverflowのコードを書式化しようとします。 皆様のご支援に感謝します:) –

+0

マーク、私たちが手伝ってくれてうれしいです。良いコーディングスタイルと幸運を実践してください。役に立つマーク/アップヴォートの答え:) –

関連する問題