题目描述
编写一个控制台程序。以控制台方式输入整数,且调用Class1类CompareNum方法判断是否猜中,给出大了、小了、猜中三种提示。输入exit表示输入结束。
输入
无
输出
太小了
太大了
猜中了
提示
若输入的既不是数字,又不是exit,应给出合理提示。如请输入数字!
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);
}
}
}
}