c# loop until Console.ReadLine = 'y' or 'n'

后端 未结 2 498
醉话见心
醉话见心 2021-01-24 10:52

I\'m fairly new to c#, and writing a simple console app as practice. I want the application to ask a question, and only progress to the next piece of code when the user input eq

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-24 11:20

    If you want to compare a character, then their is not need for ReadLine you can use ReadKey for that, if your condition is this :while ((wantCountLower != 'y') || (wantCountLower != 'n')); your loop will be an infinite one, so you can use && instead for || here or it will be while(wantCount!= 'n') so that it will loops until you press n

    char charYesOrNo;
    do
    {
       charYesOrNo = Console.ReadKey().KeyChar;
       // do your stuff here
    }while(char.ToLower(charYesOrNo) != 'n');
    

提交回复
热议问题