【发布时间】:2012-08-13 11:35:17
【问题描述】:
exit 方法在按下 q 时不起作用,并带有异常句柄 error.it 是一个控制台应用程序,我应该同时使用 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 个数字?
-
是的,我在函数中传递了两个参数。但是有什么办法可以解决这个问题吗?
-
也可以使用 ToUpper(Char) 或 ToLower(Char)。省去您检查大小写的麻烦。
-
@Science_Fiction 除非 Char 显示为空......