OJ Problem 3484 Guess the number (C#)

Title description

Write a console program. Enter the integer in the console mode, and call the CompareNum method of Class1 to determine whether the guess is correct, and three prompts are given: big, small, and guessed. Enter exit to indicate the end of the input.

enter

no 

Output

Too small 
too big 
guessed 

prompt

If the input is neither a number nor an exit, a reasonable prompt should be given. If please enter a number!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Class1
    {
        internal static void CompareNum(int num)
        {
            //5.随机生成一个数
            Random rd = new Random();
            int x = rd.Next(1, 100);
            //6.将控制台输入的数与随机生成的数进行比较,并输出结果
            if(num>x)
            {
                Console.WriteLine("太大了");
            }
            else if(num<x)
            {
                Console.WriteLine("太小了");
            }
            else
            {
                Console.WriteLine("猜中了");
            }
        }
    }
    class Program
    {      
        static void Main(string[] args)
        {
            int num;
            while (true)
            {
                //1.输入一行数据
                string s = Console.ReadLine();
                //2.若输入为“exit”,结束
                if (s == "exit")
                {
                    break;
                }
                //3.将输入转化为数字,若输入不合法,输出提示信息
                bool b = int.TryParse(s,out num);
                if(b==false)
                {
                    Console.WriteLine("请输入数字!");
                    continue;
                }
                //4.调用函数
                Class1.CompareNum(num);
            }
        }
    }
}

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/104823416