控制台应用程序退出方法无效

时间:2012-08-13 11:35:17

标签: c# .net methods exit

按下q返回异常句柄错误时,

退出方法不起作用。控制台应用程序应该同时使用application.exit()和envoirnment.exit()。两者都不起作用。难道我做错了什么 。帮助将非常感激。

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

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = 0;

            while (true)
            {
                Console.WriteLine("Press A for addition");
                Console.WriteLine("Press S for subtraction");
                Console.WriteLine("Press M for Multiplication");
                Console.WriteLine("Press D for Divide");
                Console.WriteLine("Press q for Exit");


                char c = Convert.ToChar(Console.ReadLine());

                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());

                switch (c)
                {
                    case 'A':
                    case 'a':
                        {
                            d = add(a, b);
                            Console.WriteLine(d);
                            break;


                        }


                    case 'S':
                    case 's':
                        {
                            d = sub(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'M':
                    case 'm':
                        {
                            d = mul(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'D':
                    case 'd':
                        {
                            d = div(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'q':
                        {

                            Environment.Exit(0);
                            break;

                        }
                    default:
                        {

                            Console.WriteLine("Please Enter the correct Character");
                            break;
                        }



                }
            }
        }
            private static int add(int a, int b)
    {

                   return a + b;
    }
               private static int sub(int a, int b)
    {

                   return a - b;
    }
               private static int mul(int a, int b)
    {
                   return a * b;
    }
               private static int div(int a, int b)
    {

                   return a / b;
    }



        }
    }

确定这项工作感谢大家的帮助,如果代码完美且我没有遗漏某些东西,请检查并告诉我。谢谢

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

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = 0;

            while (true)
            {
                Console.WriteLine("Press A for addition");
                Console.WriteLine("Press S for subtraction");
                Console.WriteLine("Press M for Multiplication");
                Console.WriteLine("Press D for Divide");
                Console.WriteLine("Press q for Exit");


                char c = Convert.ToChar(Console.ReadLine());
                if (c == 'q')
                {
                    Environment.Exit(0);
                }
                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());

                switch (c)
                {
                    case 'A':
                    case 'a':
                        {
                            d = add(a, b);
                            Console.WriteLine(d);
                            break;


                        }


                    case 'S':
                    case 's':
                        {
                            d = sub(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'M':
                    case 'm':
                        {
                            d = mul(a, b);
                            Console.WriteLine(d);
                            break;
                        }

                    case 'D':
                    case 'd':
                        {
                            d = div(a, b);
                            Console.WriteLine(d);
                            break;
                        }


                    default:
                        {

                            Console.WriteLine("Please Enter the correct Character");
                            break;
                        }



                }
            }
        }
            private static int add(int a, int b)
    {

                   return a + b;
    }
               private static int sub(int a, int b)
    {

                   return a - b;
    }
               private static int mul(int a, int b)
    {
                   return a * b;
    }
               private static int div(int a, int b)
    {

                   return a / b;
    }



        }
    }

5 个答案:

答案 0 :(得分:4)

也许是因为您的应用程序在按“q”后等待2个数字?

尝试更改代码,删除案例“q”并替换:

char c = Convert.ToChar(Console.ReadLine());

以下内容:

char c = Convert.ToChar(Console.ReadLine());
if(c.ToLower() == 'q')
{
    Environment.Exit(0);
}

答案 1 :(得分:1)

char c = Convert.ToChar(Console.ReadLine());
// you will need to Exit here if char is 'q'
// else it is expecting to read more entries below before it reaches your "case"
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());

答案 2 :(得分:0)

尝试Environment.Exit()这应该可以正常工作。

答案 3 :(得分:0)

这段代码

char c = Convert.ToChar(Console.ReadLine()); 
int a = Convert.ToInt32(Console.ReadLine()); 
int b = Convert.ToInt32(Console.ReadLine()); 

从控制台读取3行。

你可能想要

char c = Convert.ToChar(Console.ReadLine()); 
if (c=='Q' or c=='q') return;
int a = Convert.ToInt32(Console.ReadLine()); 
int b = Convert.ToInt32(Console.ReadLine()); 

答案 4 :(得分:-1)

只需更改您的代码

bool exitLoop = false;
while(exitLoop) {
    ...
    case 'q':
        {
             exitLoop = true;
             break;
        }
    ...
}