C#小游戏-------猜数字(转载)

朋友们,刚编写完这个小游戏,已经11点多了.休息一下,欣赏一下.学习任何的语言,都是在不断的编写中提高了.勿以简小而不为.using System;
public class Game
{
    private string number;
    private static int x,y;
    private int count=0;
    
    public void init()
    {
      do{
      Console.WriteLine("Please input number:");
      number=(string)Console.ReadLine();
      }while(number.Length!=4);
    }
    public void play()
    {
     string ready="1234";
//这里ready的值是要随机变化的.否则,没人玩你的游戏了.
//char[] r=new char[]{@#0@#,.......@#9@#};
// 用循环和随机函数将其顺序打乱.在取其前4个元素,给ready就可以了.
     do{
        x=0;
        y=0;
        init();
        for(int i=0;i<=3;i++)
        {
           for(int j=0;j<=3;j++)
           {
              if(number[i]==ready[j])
              {
                 if (i==j)
                 ++x;
                 else
                 ++y;
              }
           }
         }
     ++count;   
     Console.WriteLine("{0}A{1}B",x.ToString(),y.ToString());
      if(count>=8)
      {
       Console.WriteLine("-------------------");
       Console.WriteLine("You already lossed");
       Console.WriteLine("-------------------");
       return;
       }
     }while(number!=ready);
      Console.WriteLine("----------------------------");
      Console.WriteLine("OK! You are very cleverness!");
      Console.WriteLine("----------------------------");

   }
}
public class MyGame
{
   public static void Main()
{
   Game g=new Game();
   g.play();
}
}
已经编译通过.

转载于:https://www.cnblogs.com/9who/archive/2007/08/30/876190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值