完成计算后保持循环

时间:2017-03-02 22:21:02

标签: c#

static void Main(string[] args)
    {
        double a, b, c, Result;
        while (true) // and is  good idea to use while to keep backing to main ??
        {
                Console.WriteLine("\t\t\t\t\t\t\tCalcualtor");
                Console.WriteLine("\t\t\t\t\t\t\t----------");
            Console.WriteLine("put Number Plz");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("put other Number Plz");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Choose :*,+,-,/");
            string uservalue = Console.ReadLine();
            if (uservalue == "*")
            {
                Result = a * b;
                Console.Write("Resultat= " + a * b);
            }
            else if (uservalue == "+")
            {
                Result = a + b;
                Console.Write("Resultat= " + (a + b));
            }
            else if (uservalue == "-")
            {
                Result = a - b;
                Console.Write("Resultat= " + (a - b));
            }
            else if (uservalue == "/")
            {
                Result = a / b;
                Console.Write("Resultat= " + (a / b));
            }
            else
                Console.WriteLine("put right operator !!!");

//我需要在这里开始我在做什么之前做的意思是循环我还是新的c#任何建议?

            Console.ReadLine();
            Console.Clear();
        }
    }
}

}

这个问题我早就问过了,我对c#了解不多,所以我已经解决了这个问题

3 个答案:

答案 0 :(得分:0)

如果您正在寻找一个全新的解决方案,您可能希望在成功操作时引发一个标志,并将操作员输入包装到另一个循环中:

bool successful;
do
{
    string uservalue = Console.ReadLine();
    if (uservalue == "*")
    {
        Result = a * b;
        Console.Write("Resultat= " + a * b);
        successful = true;
    }
    //other operators
    else
        Console.WriteLine("put right operator !!!");
} while (!successful)

另外我会建议切换/案例结构,如:

string uservalue = Console.ReadLine();
switch (uservalue)
{
    case "*":
        Console.Write("Resultat= " + (a * b));
        break;
    //other operators
    default:
        Console.WriteLine("put right operator !!!");
        break;
}

PS:你计算两次值,一次是为变量“Result”赋值,另一次是在输出字符串时,如果你已经在变量中有值,你应该致电Console.Write("Resultat= " + Result);

答案 1 :(得分:0)

我会创建函数来读取用户输入:

static double GetNumber(string prompt) {
    double answer;
    Console.WriteLine(prompt);
    while (true) {
        if (double.TryParse(Console.ReadLine(), out answer)) {
            return answer;
        }
       Console.WriteLine("Error: Try again");
    }
}
static string GetOperator(string prompt) {
    string answer;
    Console.WriteLine(prompt);
    while (true) {
        answer = Console.ReadLine();
        if ("*/+-".Contains(answer)) {
            return answer;
        }
       Console.WriteLine("Error: Try again");
    }
}

然后你的主要成为:

static void Main(string[] args)
{
    while (true)
    {
        Console.WriteLine("\t\t\t\t\t\t\tCalcualtor");
        Console.WriteLine("\t\t\t\t\t\t\t----------");
        double a = GetNumber("put Number Plz");
        double b = GetNumber("put other Number Plz");
        string uservalue = GetOperator("Choose :*,+,-,/");
        if (uservalue == "*")
        {
            Console.Write("Resultat= " + a * b);
        }
        else if (uservalue == "+")
        {
            Console.Write("Resultat= " + (a + b));
        }
        else if (uservalue == "-")
        {
            Console.Write("Resultat= " + (a - b));
        }
        else if (uservalue == "/")
        {
            Console.Write("Resultat= " + (a / b));
        }
        Console.ReadLine();
        Console.Clear();
    }

答案 2 :(得分:0)

它的工作现在很完美

 static void Main(string[] args)
    {
        double a, b,Result;
        while (true)
        {
            Console.WriteLine("\t\t\t\t\t\t\tCalcualtor");
            Console.WriteLine("\t\t\t\t\t\t\t----------");
            Console.WriteLine("put Number Plz");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("put other Number Plz");
            b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Choose :*,+,-,/");
            bool successful;
            do
            {
                string uservalue = Console.ReadLine();
                if (uservalue == "*")
                {
                    Result = a * b;
                    Console.Write("Resultat= " + a * b);
                    successful = true;
                }
                else if (uservalue == "+")
                {
                    Result = a + b;
                    Console.Write("Resultat= " + (a + b));
                    successful = true;
                }
                else if (uservalue == "-")
                {
                    Result = a - b;
                    Console.Write("Resultat= " + (a - b));
                    successful = true;
                }
                else if (uservalue == "/")
                {
                    Result = a / b;
                    Console.Write("Resultat= " + (a / b));
                    successful = true;
                }
                else 
                {
                  Console.WriteLine("put right operator !!!");
                    successful = false ;
                }


               } 
            while (!successful);
            {
            Console.ReadLine();
            Console.Clear();
            }

           }
       }    
    }
}