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;
}
}
}
也许是因为您的应用程序在按"q"后等待 2 个数字?
尝试更改代码,删除大小写"Q"并替换:
char c = Convert.ToChar(Console.ReadLine());
与以下内容:
char c = Convert.ToChar(Console.ReadLine());
if(c.ToLower() == 'q')
{
Environment.Exit(0);
}
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());
尝试Environment.Exit()
这应该正常工作。
这段代码
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());
只需像这样更改代码即可
bool exitLoop = false;
while(exitLoop) {
...
case 'q':
{
exitLoop = true;
break;
}
...
}