请教C#的Console.ReadLine()问题
我写了一个小程序,用于判断任一输入数据是否是素数,程序看上去没有问题,编译也没问题,但在执行时出错,我没找到原因。我只发现这个程序的while循环中的string p = Console.ReadLine();语句,在第一次循环中能正确执行,读取从控制台键入的字符。但是当进入第二次循环时,该语句在我没有向控制台敲入任何字符的情况下(也没有按回车),执行了,且自动将一空字符窜""
赋值给局部变量p,然后根据程序代码逻辑就抛出异常,结束程序运行。
我想请教各位高人,给我解答解答,在下先谢过了。程序代码如下:
using System;
namespace ConsoleApplication2
{
class Class1
{
public static bool abc(int a)
{
bool x = true;
for (int i = 2; i < a; i++)
{
if (a % i == 0) x = (!true);
}
return x;
}
[STAThread]
static void Main(string[] args)
{
//string p;
bool aa = true;
while (aa)
{
Console.WriteLine("请输入一个大于2的整数:");
string p = Console.ReadLine();
try
{
//FileClose();
int e = int.Parse(p);
if (abc(e)) Console.WriteLine("你输入的数是素数!");
else Console.WriteLine("你输入的数不是素数!");
Console.WriteLine("是否继续输入数(y/n)");
char xx = (char)Console.Read();
if (xx == 'n'|| xx == 'N') aa = (!true);
}
catch (System.Exception E)
{
Console.WriteLine("你输入的数据数型错误!" + E);
Console.WriteLine(p);
aa = (!true);
}
}
}
}
}