Advertisement
TwinFrame

WhileEnterExit

Dec 9th, 2020 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace WhileEnterExit
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int initialValue = 0;
  10. int finalValue = 10;
  11. bool isOpen = true;
  12. bool isNumber = false;
  13. string userInput;
  14. int currentNumber;
  15. Random random = new Random();
  16.  
  17. Console.WriteLine($"Угадайте число от {initialValue} до {finalValue}. Напишите \"exit\" для выхода.");
  18.  
  19. while (isOpen)
  20. {
  21. currentNumber = random.Next(0, 11);
  22. Console.Write("Ваше число: ");
  23. userInput = Console.ReadLine();
  24. isNumber = Int32.TryParse(userInput, out int value);
  25.  
  26. if (isNumber && value >= initialValue && value <= finalValue)
  27. {
  28. if (value == currentNumber)
  29. {
  30. Console.WriteLine("Поздравляю, вы угадали!");
  31. }
  32. else
  33. {
  34. Console.WriteLine($"Вы не угадали! Загаданное число: {currentNumber}");
  35. }
  36. }
  37. else if (userInput == "exit")
  38. {
  39. isOpen = false;
  40. }
  41. else
  42. {
  43. Console.WriteLine($"Некорректный ввод. Введите число от {initialValue} до {finalValue}. Напишите \"exit\" для выхода.");
  44. }
  45. }
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement