繁体   English   中英

Visual Studio 2015 社区 C# 计算器代码错误

[英]Visual Studio 2015 Community C# Calculator Code Errors

我写了一个测试示例代码,但我遇到了问题。 编码'

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 循环,通过控制台,要求用户输入两个数字,然后是一个运算符,然后执行并显示计算结果。 他们继续询问用户是否愿意再试一次。 它使用choice = Console.ReadLine()语句。 根据答案,代码要么继续 while 循环,要么跳出循环。 不幸的是,编译器似乎跳过了那个选择部分,直接进入了 switch 语句。 代码顶部有类似的语句,它们工作正常。

任何想法将不胜感激。

这是因为,您在第 34 行使用了 Console.Read()。它读取一个字符,当您按 ENTER 时,它还会读取返回键。 所以在缓冲区中已经有一个键,它跳过 ReadLine() 并将其作为换行读取。

如果您将op更改为字符串,并且您使用ReadLine()而不是Read() ,那么这将确保您的输入流中没有挂起/隐藏的字符。 然后,您的开关检查op是否是字符串"+""-""*" .. 等,而不是字符'+''-' .. 等。

我还修复了你的缩进。 请在您未来的所有实现中遵循这种风格,以便更好地阅读和理解:

这应该有效:

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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM