C#switch语句字符串为char错误

时间:2015-08-19 05:50:07

标签: c# switch-statement

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

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

4 个答案:

答案 0 :(得分:2)

你宣布

char action;

作为char Type但

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 

char action;替换为string action;

答案 1 :(得分:1)

使用

action = Console.ReadKey().KeyChar;

而不是

action = Console.ReadLine();

&安培;

case "+":case '+':

答案 2 :(得分:1)

您可能会发现删除swicth并实现词典非常有用:

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");

如果你有很多动作(例如 power **提醒 %等等。字典实现更具可读性。

答案 3 :(得分:0)

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();
        }
    }
}
}
相关问题