在c#中循环时无限

时间:2015-03-12 10:37:07

标签: c#

我是c#的新手,我遇到了一个不会中断的while循环问题。它只是一个简单的控制台计算器,如果输入正确,我希望switch语句突破循环并保持循环。问题是,即使选择了正确的字母,它也会保持循环,例如A代表添加,这是我的代码:

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

namespace basic_calc
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("this is a basic calculator");
            double num1 = Getvalue("Enter the first number: ");
            double num2 = Getvalue("Enter the second number: ");

            double output = 0;
            while (true)
            {
                Console.WriteLine(" Please select an operation (A)dd, (S)ubtract, (M)ultiply, (D)ivide");
                char operation = Console.ReadKey().KeyChar;
                operation = Char.ToUpper(operation);
                switch (operation)
                {
                    case 'A':
                        output = Add(num1, num2);
                        break;
                    case 'S':
                        output = Subtract(num1, num2);
                        break;
                    case 'M':
                        output = Multiply(num1, num2);
                        break;
                    case 'D':
                        output = Divide(num1, num2);
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
                        Console.ResetColor();
                        continue;
                }    
            }


            Console.WriteLine("\nresult: "+ output);

            Console.ReadKey();
        }
        private static double Getvalue(string input)
        {
            //the value to be returned
            double value;
            while (true)
            {
                Console.Write(input);
                string number = Console.ReadLine();
                if (double.TryParse(number, out value))
                {
                    return value;

                }
                else
                    Console.WriteLine("please enter a number for this calculator");
            }
        }
        private static double Add(double n1, double n2){return n1 + n2;}
        private static double Subtract(double n1, double n2) { return n1 - n2; }
        private static double Multiply(double n1, double n2) { return n1 * n2; }
        private static double Divide(double n1, double n2) { return n1 / n2; }
    }
}

3 个答案:

答案 0 :(得分:6)

只需在break之后添加switch

switch (operation)
{
    case 'A':
        output = Add(num1, num2);
        break;
    case 'S':
        output = Subtract(num1, num2);
        break;
    case 'M':
        output = Multiply(num1, num2);
        break;
    case 'D':
        output = Divide(num1, num2);
        break;
    default:
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
        Console.ResetColor();
        continue;
}
break; // This will exit the loop when a match is found.

由于如果找不到匹配项,您使用的是continue,如果找到匹配项,则break之后的switch将退出循环。

答案 1 :(得分:3)

您的break刚跳出开关声明。尝试使用您在案例中设置的bool变量:

var noOutput = true;
while (noOutput)
            {
                Console.WriteLine(" Please select an operation (A)dd, (S)ubtract, (M)ultiply, (D)ivide");
                char operation = Console.ReadKey().KeyChar;
                operation = Char.ToUpper(operation);
                switch (operation)
                {
                    case 'A':
                        output = Add(num1, num2);
                        noOutput = false;
                        break;
                    ...
                }    
            }

答案 2 :(得分:2)

你必须打破switch语句。因为While(true)你最终处于循环中。所以在你的switch语句后加上break

switch (operation)
{
case 'A':
    output = Add(num1, num2);
    break;
case 'S':
    output = Subtract(num1, num2);
    break;
case 'M':
    output = Multiply(num1, num2);
    break;
case 'D':
    output = Divide(num1, num2);
    break;
default:
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("INVALID INPUT!!!!!!!!!!!!!!!");
    Console.ResetColor();
    continue;
}
  break;