如何在ReadLine(C#)中只接受有效选项?

huangapple go评论105阅读模式
英文:

How to only accept valid options in ReadLine (C#)

问题

Program.cs

  1. namespace DrivingLicenseExam
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Questions firstQuestion = new Questions("1. You may drive off the paved roadway to pass another vehicle: ", "c", "");
  8. Console.WriteLine(firstQuestion.question);
  9. Console.WriteLine("----");
  10. Console.WriteLine("a) - If the shoulder is wide enough to accommodate your vehicle.");
  11. Console.WriteLine("b) - If the vehicle ahead of you is turning left.");
  12. Console.WriteLine("c) - Under no circumstances.");
  13. Console.Write("Your answer is: ");
  14. firstQuestion.UserAnswer = Console.ReadLine().ToLower();
  15. if (firstQuestion.UserAnswer == firstQuestion.correctAnswer)
  16. {
  17. totalScore++;
  18. }
  19. }
  20. }
  21. }

Questions.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace DrivingLicenseExam
  7. {
  8. class Questions
  9. {
  10. public string question;
  11. public string correctAnswer;
  12. private string userAnswer;
  13. public Questions(string aQuestion, string aCorrectAnswer, string aUserAnswer)
  14. {
  15. question = aQuestion;
  16. correctAnswer = aCorrectAnswer;
  17. UserAnswer = aUserAnswer;
  18. }
  19. public string UserAnswer
  20. {
  21. get { return userAnswer; }
  22. set {
  23. if (value == "a" || value == "b" || value == "c")
  24. {
  25. userAnswer = value;
  26. }
  27. else
  28. {
  29. Console.WriteLine("Invalid Answer. - Please choose from 'a', 'b' or 'c'.");
  30. }
  31. }
  32. }
  33. }
  34. }

The code you provided is now free of HTML entity encoding for quotation marks, and the Chinese translation has been removed, as you requested.

英文:

This Driving License Exam project would be my 4th code after starting to learn programming.

Program.cs

  1. namespace DrivingLicenseExam
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Questions firstQuestion = new Questions("1. You may drive off the paved roadway to pass another vehicle: ", "c", "");
  8. Console.WriteLine(firstQuestion.question);
  9. Console.WriteLine("----");
  10. Console.WriteLine("a) - If the shoulder is wide enough to accommodate your vehicle.");
  11. Console.WriteLine("b) - If the vehicle ahead of you is turning left.");
  12. Console.WriteLine("c) - Under no circumstances.");
  13. Console.Write("Your answer is: ");
  14. firstQuestion.UserAnswer = Console.ReadLine().ToLower();
  15. if (firstQuestion.UserAnswer == firstQuestion.correctAnswer)
  16. {
  17. totalScore++;
  18. }
  19. }
  20. }
  21. }

Questions.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace DrivingLicenseExam
  7. {
  8. class Questions
  9. {
  10. public string question;
  11. public string correctAnswer;
  12. private string userAnswer;
  13. public Questions(string aQuestion, string aCorrectAnswer, string aUserAnswer)
  14. {
  15. question = aQuestion;
  16. correctAnswer = aCorrectAnswer;
  17. UserAnswer = aUserAnswer;
  18. }
  19. public string UserAnswer
  20. {
  21. get { return userAnswer; }
  22. set {
  23. if (value == "a" || value == "b" || value == "c")
  24. {
  25. userAnswer = value;
  26. }
  27. else
  28. {
  29. Console.WriteLine("Invalid Answer. - Please choose from \"a\", \"b\" or \"c\".");
  30. }
  31. }
  32. }
  33. }
  34. }

I am using objects to create the questions with the valid answers and I want the user to only be able to enter "a", "b" or "c", otherwise it should say:

  1. Console.WriteLine("Invalid answer.");
  2. Console.WriteLine("Please choose between \"a\", \"b\" or \"c\".");

This is an example from a previous project but I am not sure how to integrate it in this one:

  1. do
  2. {
  3. string measureUnit = Console.ReadLine().ToLower();
  4. if (measureUnit == "km" || measureUnit == "cm" || measureUnit == "m")
  5. {
  6. return measureUnit;
  7. }
  8. Console.WriteLine("Incorrect measurement");
  9. Console.Write("Choose: ");
  10. } while (true);

Thank you! 如何在ReadLine(C#)中只接受有效选项?

答案1

得分: 1

以下是您提供的内容的翻译:

"我能想到的最简单方法是创建一个验证方法和一个在问题类中验证答案的方法。

下面的主程序中的 while 循环不断调用“ValidateAnswer”方法,直到收到有效输入为止。

  1. class Questions
  2. {
  3. public string question;
  4. public string correctAnswer;
  5. private string userAnswer;
  6. public Questions(string aQuestion, string aCorrectAnswer)
  7. {
  8. question = aQuestion;
  9. correctAnswer = aCorrectAnswer;
  10. }
  11. public bool ValidateAnswer(string aUserAnswer)
  12. {
  13. if (aUserAnswer == "a" || aUserAnswer == "b" || aUserAnswer == "c")
  14. {
  15. return true;
  16. }
  17. Console.WriteLine("无效答案。");
  18. Console.WriteLine("请选择'a'、'b'或'c'之间的一个。");
  19. return false;
  20. }
  21. public bool VerifyAnswer(string aUserAnswer)
  22. {
  23. return (aUserAnswer == correctAnswer);
  24. }
  25. static void Main(string[] args)
  26. {
  27. Questions firstQuestion = new Questions("1. 您可以驶离铺设的道路以超越另一辆车:", "c");
  28. Console.WriteLine(firstQuestion.question);
  29. Console.WriteLine("----");
  30. Console.WriteLine("a) - 如果路肩足够宽以容纳您的车辆。");
  31. Console.WriteLine("b) - 如果前面的车正在左转。");
  32. Console.WriteLine("c) - 无论如何都不可以。");
  33. Console.Write("您的答案是:");
  34. string answer = Console.ReadLine().ToLower();
  35. while (!firstQuestion.ValidateAnswer(answer))
  36. {
  37. answer = Console.ReadLine().ToLower();
  38. }
  39. if (firstQuestion.VerifyAnswer(answer))
  40. {
  41. totalScore++;
  42. }
  43. }
  44. }

请注意,我已经保留了代码部分不进行翻译,只翻译了注释和字符串内容。

英文:

The easiest way I can think of is to create a method to validate and a method to verify the answer within the questions class.

The while loop in the Main program below keeps calling the "ValidateAnswer" method until a valid input is received.

  1. class Questions
  2. {
  3. public string question;
  4. public string correctAnswer;
  5. private string userAnswer;
  6. public Questions(string aQuestion, string aCorrectAnswer)
  7. {
  8. question = aQuestion;
  9. correctAnswer = aCorrectAnswer;
  10. }
  11. public bool ValidateAnswer(string aUserAnswer)
  12. {
  13. if (aUserAnswer == "a" || aUserAnswer == "b" || aUserAnswer == "c")
  14. {
  15. return true;
  16. }
  17. Console.WriteLine("Invalid answer.");
  18. Console.WriteLine("Please choose between \"a\", \"b\" or \"c\".");
  19. return false;
  20. }
  21. public bool VerifyAnswer(string aUserAnswer)
  22. {
  23. return (aUserAnswer == correctAnswer);
  24. }
  25. static void Main(string[] args)
  26. {
  27. Questions firstQuestion = new Questions("1. You may drive off the paved roadway to pass another vehicle: ", "c");
  28. Console.WriteLine(firstQuestion.question);
  29. Console.WriteLine("----");
  30. Console.WriteLine("a) - If the shoulder is wide enough to accommodate your vehicle.");
  31. Console.WriteLine("b) - If the vehicle ahead of you is turning left.");
  32. Console.WriteLine("c) - Under no circumstances.");
  33. Console.Write("Your answer is: ");
  34. string answer = Console.ReadLine().ToLower();
  35. while (!firstQuestion.ValidateAnswer(answer))
  36. {
  37. answer = Console.ReadLine().ToLower();
  38. }
  39. if (firstQuestion.VerifyAnswer(answer))
  40. {
  41. totalScore++;
  42. }
  43. }
  44. }

答案2

得分: 0

作为对Tom Hazell精彩回答的补充,我想提供一个更通用的ValidateAnswer方法:

  1. public bool ValidateAnswer(string aUserAnswer, string[] possibleAnswers)
  2. {
  3. if (possibleAnswers.Contains(aUserAnswer))
  4. {
  5. return true;
  6. }
  7. Console.WriteLine("无效答案。");
  8. var options = string.Join(", ", possibleAnswers);
  9. Console.WriteLine($"请选择在{options}之间。");
  10. return false;
  11. }

这个方法的思路是传递有效的答案字符数组,以应对问题可能有多于三个答案的情况。

然后你可以这样调用它:

  1. string answer = Console.ReadLine().ToLower();
  2. while (!firstQuestion.ValidateAnswer(answer, new string[] {"a", "b", "c"}))
  3. {
  4. answer = Console.ReadLine().ToLower();
  5. }

使用Contains需要引入using System.Linq;

如果你坚持要有更友好的错误消息,你可以这样做:

  1. var first = string.Join(", ", possibleAnswers.Take(possibleAnswers.Count() - 1));
  2. var last = possibleAnswers[possibleAnswers.Count() - 1];
  3. Console.WriteLine($"请选择在{first}和{last}之间。");

所有这些方法都仅在possibleAnswers包含多个元素时才有效,但对于一个问题只有1个或0个答案的情况是没有意义的。

英文:

As an addendum to Tom Hazell's fine answer I would make a more generic ValidateAnswer:

  1. public bool ValidateAnswer(string aUserAnswer, string[] possibleAnswers)
  2. {
  3. if (possibleAnswers.Contains(aUserAnswer)
  4. {
  5. return true;
  6. }
  7. Console.WriteLine("Invalid answer.");
  8. var options = string.Join(", ", possibleAnswers);
  9. Console.WriteLine($"Please choose between {options}");
  10. return false;
  11. }

The idea is to pass the valid characters, if a question might have more than three answers.

Then you would call it like:

  1. string answer = Console.ReadLine().ToLower();
  2. while (!firstQuestion.ValidateAnswer(answer, new string {"a","b","c"}))
  3. {
  4. answer = Console.ReadLine().ToLower();
  5. }

Contains requires using System.Linq;.

If you insist on the nicer formulation of the error message, you can do:

  1. var first = string.Join(", ", possibleAnswers.Take(possibleAnswers.Count() - 1));
  2. var last = possibleAnswers[possibleAnswers.Count() - 1];
  3. Console.WriteLine($"Please choose between {first} and {last}.");

All of this only works if possibleAnswers contains more than one element, but having 1 or 0 answers to a question wouldn't make sense.

huangapple
  • 本文由 发表于 2023年6月9日 12:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定