3月10日C#作业猜单词 夏威夷

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

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            int Max_guess=3;
            string[] a = { "apple", "banana","orange" };
            Random b=new Random();
            int c = b.Next(0, 2);
            string myString = a[c];
            char[] My_char = myString.ToCharArray();
            Console.WriteLine("请输入猜测的字符");

            for (int i = 0; i <= myString.Length-1; i++)
            {
                Console.Write("-");
            }
            Console.Write("\n");
            while (true)
            {
                Console.WriteLine("剩余猜测次数为{0}", Max_guess);
                string guess = Console.ReadLine();
                int count = 0;
                if (guess.Length == myString.Length)
                {
                   
                    foreach (char ch in guess)
                    {
                        if (My_char[count] == ch)
                        {
                            Console.Write(ch);
                        }
                        else
                        {
                            Console.Write("-");
                        }

                        count++;
                    }
                    Max_guess--;
                    Console.Write("\n");
                }
                else
                {
                    Console.WriteLine("输入与猜测单词长度不匹配");
                }
           
                if (myString == guess)
                {
                    Console.WriteLine("猜测正确,玩家获胜");
                    break;
                }
                if (Max_guess <= 0)
                {
                    Console.WriteLine("猜测超过上限,机器获胜");
                    break;
                }

            }
        }
    }
}

posted @ 2017-03-13 15:25  weiweiyi  阅读(80)  评论(0)    收藏  举报