namespace ClassesnObject
{
class Program
{
public class myClass
{
string val;
public static int val2 = 0;
public void bottle(string name)
{
val = name;
val2++;
}
}
static ConsoleKeyInfo readkey = new ConsoleKeyInfo();
static myClass myObj = new myClass();
static void input() //This is the problematic method
{
string name;
bool con = true;
Console.WriteLine("Enter name: ");
name = Console.ReadLine();
myObj.bottle(name);
while (con)
{
Console.WriteLine("Want to enter more name(Y/N)? ");
readkey = Console.ReadKey();
if (readkey.KeyChar == 'Y' || readkey.KeyChar == 'y') input();
else if (readkey.KeyChar == 'N' || readkey.KeyChar == 'n') return;//Problem
else continue;
}
}
static void Main(string[] args)
{
input();
Console.WriteLine("No. of entries are: " + myClass.val2);
Console.ReadLine();
}
}
当我在 input() 方法中,并且在 while 循环中按 'Y' 或 'y' 完成工作,但 'N' 或 'n' 没有。似乎在按下“N”或“n”时它不会返回,直到我们按下“N”或“n”我们输入名称的次数。