C#将语句字符串切换为字符错误

C#将语句字符串切换为字符错误,c#,switch-statement,C#,Switch Statement,我试图让这个switch语句工作,但它不断出现错误,无法将string更改为char。我看不出我在哪里尝试将字符串更改为char。您声明了吗 using System; namespace prac4b { class prac4b { static void Main(string[] args) { int number1, number2, result; char action;

我试图让这个switch语句工作,但它不断出现错误,无法将string更改为char。我看不出我在哪里尝试将字符串更改为char。

您声明了吗

using System;
namespace prac4b
{
    class prac4b
    {
        static void Main(string[] args)
        {
            int number1, number2, result;
            char action;
            string tempVal = "";
            bool parseAttempt = false;

            // ask for first number
            Console.ReadLine();
            Console.Write("Enter number > ");

            //testing if integer with TryParse
            tempVal = Console.ReadLine();
            parseAttempt = Int32.TryParse(tempVal, out number1);

            // if not a number
            if (parseAttempt == false)
            {
                Console.WriteLine("You have not entered a number, application will now exit.");
                Environment.Exit(0);
            }
            //if true, continue, ask for number2
            if (parseAttempt == true)
            {
                //asking for number
                Console.Write("Enter another number > ");
                tempVal = Console.ReadLine(); //storing number temporailiy for checking
                parseAttempt = Int32.TryParse(tempVal, out number2); //checking number2 if integer

                //if not a number
                if (parseAttempt == false)
                {
                    Console.WriteLine("ERROR you have not entere a valid integer");
                    Environment.Exit(0);
                }
                //if true, continue, ask for action(+*-+)
                Console.WriteLine("Action (*/+-) > ");
                action = Console.ReadLine();

                switch (action) //switch statement for action list
                {
                    case "+":
                        Console.WriteLine("Result is > ", number1 + number2);
                        break;
                    case "-":
                        Console.WriteLine("Result is > ", number1 - number2);
                        break;
                    case "*":
                        Console.WriteLine("Result is > ", number1 * number2);
                        break;
                    case "/":
                        Console.WriteLine("Result is > ", number1 / number1);
                        break;
                    default:
                        Console.WriteLine("ERROR INVALID INPUT");
                        Environment.Exit(0);
                        break;
                }
                Console.WriteLine();
            }
        }
    }
}
作为字符类型,但是

char action;
替换
char动作带有
字符串动作

使用

switch (action)
{
    case "+": // here you compare it with a string. 
    ....
    case "-": // here you compare it with a string.
    ....
    case "*": // here you compare it with a string.
    ....
    case "/": // here you compare it with a string.
...
action = Console.ReadLine(); //here you try to set a string 
而不是

action = Console.ReadKey().KeyChar;
&


case“+”:
case“+”:

您可能会发现删除
开关并实现字典非常有用:

action = Console.ReadLine();
字典操作=新建{
{“+”,(x,y)=>x+y},
{-”,(x,y)=>x-y},
{“*”,(x,y)=>x*y},
{”/(x,y)=>x/y},
};
...
Console.WriteLine(“操作(*/+-)>”;
action=Console.ReadLine();
Func Func;
if(Actions.TryGetValue(action,out func))
WriteLine(“结果是>”,func(number1,number2));
其他的
Console.WriteLine(“错误无效输入”);

如果您有很多操作(例如power
**
、reminder
%
等),则字典实现更具可读性。

考虑到他只有一个字符的大小写,哈希特的答案可能更好。我会用Dmitry Bychenko的答案来解决这个问题,但我认为这个问题更多的是理解错误,而不是找到最有效的解决方案。尽管在我发表评论时,这个问题并不存在<代码>案例“-”:
案例“*”:
案例“/”:
当然,不用说。你必须改变每一个案例陈述。
using System;

namespace prac4b
{
class prac4b
{


    static void Main(string[] args)
    {
        int number1, number2, result;
        char action;
        string tempVal = "";

        bool parseAttempt = false;

        // ask for first number
        Console.ReadLine();
        Console.Write("Enter number > ");

        //testing if integer with TryParse
        tempVal = Console.ReadLine();
        parseAttempt = Int32.TryParse(tempVal, out number1);

        // if not a number
        if (parseAttempt == false)
        {
            Console.WriteLine("You have not entered a number, application will now exit.");
            Environment.Exit(0);

        }
        //if true, continue, ask for number2
        if (parseAttempt == true)
        {
            //asking for number
            Console.Write("Enter another number > ");
            tempVal = Console.ReadLine(); //storing number temporailiy for checking
            parseAttempt = Int32.TryParse(tempVal, out number2); //checking number2 if integer

            //if not a number
            if (parseAttempt == false)
            {
                Console.WriteLine("ERROR you have not entere a valid integer");
                Environment.Exit(0);
            }
            //if true, continue, ask for action(+*-+)
            Console.WriteLine("Action (*/+-) > ");
            var readaction = Console.ReadLine();
            string actionString = readaction.ToString();
            switch (actionString) //switch statement for action list
            {
                case "+":
                    Console.WriteLine("Result is > ", number1 + number2);
                    break;
                case "-" :
                    Console.WriteLine("Result is > ", number1 - number2);
                    break;
                case "*" :
                    Console.WriteLine("Result is > ", number1 * number2);
                    break;
                case "/" :
                    Console.WriteLine("Result is > ", number1 / number1);
                    break;
                default :
                    Console.WriteLine("ERROR INVALID INPUT");
                    Environment.Exit(0);
                    break;
            }
            Console.WriteLine();
        }
    }
}
}
Dictionary<String, Func<Double, Double, Double>> Actions = new {
  {"+", (x, y) => x + y},
  {"-", (x, y) => x - y},
  {"*", (x, y) => x * y},
  {"/", (x, y) => x / y},
};

...

Console.WriteLine("Action (*/+-) > ");
action = Console.ReadLine();

Func<Double, Double, Double> func;

if (Actions.TryGetValue(action, out func))
  Console.WriteLine("Result is > ", func(number1, number2));
else
  Console.WriteLine("ERROR INVALID INPUT");