Advertisement
Misipuk

task4_DT

Dec 8th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Task4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random rand = new Random();
  14.             int k = rand.Next(101);
  15.             int ans = 0, a=0, b=100;
  16.             while (ans != k)
  17.             {
  18.                 Console.Write("Enter a number from " + a.ToString() + " to " + b.ToString() + " ");
  19.                 try
  20.                 {
  21.                     ans = int.Parse(Console.ReadLine());
  22.                     if ((ans < b) && (ans > a))
  23.                     {
  24.                         if (ans == k)
  25.                         {
  26.                             Console.WriteLine("Your answer is right");
  27.                             ans = k;
  28.                         }
  29.                         else
  30.                         {
  31.                            
  32.                             if ((ans > k))
  33.                             {
  34.                                 Console.WriteLine("Number is less");
  35.                                 b = ans;
  36.                             }
  37.                             else
  38.                             {
  39.                                 Console.WriteLine("Number is bigger");
  40.                                 a = ans;
  41.                             }
  42.                         }
  43.                     }
  44.                     else
  45.                     {
  46.                         Console.WriteLine("Number should be from " + a.ToString() + " to " + b.ToString());
  47.                     }
  48.                    
  49.                 }
  50.                 catch(Exception e)
  51.                 {
  52.                     Console.WriteLine("Your answer isn't valid");
  53.                 }
  54.             }
  55.             Console.ReadLine();
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement