【问题标题】:tell user to press only +告诉用户只按 +
【发布时间】:2022-01-11 20:11:17
【问题描述】:

我试图要求用户仅按“+”键,如果用户输入错误,请告诉他这不是操作;例如,如果用户输入数字或任何其他键,告诉用户“你必须输入一个操作” 我已经尝试了下面的代码,但没有得到修复!

Console.WriteLine("press '+' to add\npress '-' to delete");

//these are the ways i have tried:
//1:

char add = '+';
if (Console.ReadKey() == add) ;

//2:
string input = Console.ReadLine();
char inputChar = Convert.ToChar(input); 

//if user give number the program shows error to me
if (inputChar == add) ;

//3:
if (Console.ReadKey().Key == ConsoleKey.+) ;

【问题讨论】:

    标签: c# character calculator


    【解决方案1】:

    你可以尝试实现do .. while循环:

      char op;
    
      do {
        Console.WriteLine("press '+' to add\npress '-' to delete");
    
        op = Console.ReadKey().KeyChar;
      }
      while (op != '+' && op != '-');
    

    在提供有效操作 (op) 之前,我们会一直询问用户('+''-'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多