c# 循环

案例一 发牌给四个人。

52张牌面 ~~~不算大小王 

using System;
public class myApp
{
 public static void Main()
 {
  int randomum1,randomum2,flag=0;
  int[] a;
  int i,tmp,randtmp;
  a=new int[52];
  for (i=1;i<=52 ;i++ )
  {
   a[i-1]=i;
  }
  Random r1=new Random();
  for (i=0;i<52 ;i++ )
  {
   randtmp=(int)(52*r1.NextDouble());  等同于 random=r1.Next(0,52);
   tmp=a[i];
   a[i]=a[randtmp];
   a[randtmp]=tmp;
  }
  for(i=0;i<13;i++)
   Console.Write("  {0}",a[i]);
      Console.WriteLine("  player1");
   for(i=13;i<26;i++)
    Console.Write("  {0}",a[i]);
       Console.WriteLine("  player2");
                for(i=26;i<39;i++)
     Console.Write("  {0}",a[i]);
        Console.WriteLine("  player3");
           for(i=39;i<52;i++)
               Console.Write("  {0}",a[i]);
         Console.WriteLine("  player4");
                        Console.ReadKey();
 }
}

运行结果:

D:/microsoft/VC>random.exe
  28  10  5  44  25  45  16  2  15  48  30  50  27  player1
  39  32  51  14  36  42  23  37  29  3  52  1  4  player2
  38  8  26  12  49  19  46  31  11  41  20  6  21  player3
  18  22  47  43  17  9  35  24  13  33  40  7  34  player4

案例二 99乘法表的。

using System;
class Test
{
 public static void Main()
 {
  int i,j;
  for (j=1;j<=9 ;j++ )
  {
     for (i=1;i<=j ;i++ )
     {
   Console.Write("{0}*{1}={2} ",i,j,i*j);
     }
  }
  Console.ReadKey();
 }
}

这样的输出是

 

D:/microsoft/VC>99.exe
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

这样的排版好像不太正确。确实。。、我们再改改。。。

using System;
class Test
{
 public static void Main()
 {
  int i,j;
  for (j=1;j<=9 ;j++ )
  {
     for (i=1;i<=j ;i++ )
     {
   Console.Write("{0}*{1}={2} ",i,j,i*j);
     }
  Console.WriteLine();
  }
  Console.ReadKey();
 }
}

这样排版就对了。。

D:/microsoft/VC>99.exe
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

输出小写的26个字母;

大写字母的ASCII码的范围  65 90

小写字母的ASCII码的范围 97 122

using System;
class abcABC
{
 public static void Main()
 {
  char ch;
  for(int i=97;i<=122;i++)
  {
   Console.Write("{0} ",(char)i);
  }
 }
}

输出结果 a b c d e f g ..................

大写字母的 照样。。。。。。

案例  。。1!+2!+3!+4!。。。。10!

using System;
class calculate
{
 public static void Main()
 {
  int i,j,a=1,sum=0;
  for(i=1;i<=10;i++)
  {
   for(j=1;j<=i;j++)
   {
    a=a*j;
   }
   sum=sum+a;
   a=1;
  }
 
  Console.WriteLine(sum);

 }
}

运行结果。

4037913

金字塔~杨辉三角形

using System;
class YhTriangle
{
 public static void Main()
 {
  string str1=" ";
  int length;
  Console.Write("请输入杨辉三角形(金字塔)的层数/t");
  length=Int32.Parse(Console.ReadLine());
  int[] [] a=new int [length] [];
  for (int i=0;i<length ;i++ )
  {
   a[i]=new int [i+1];
  }
  for (int j=0;j<length ;j++ )
  {   int m=0;
   for ( m=0;m<=j ;m++ )
   {
    if (m==0||j==m)
    {
     a[j][m]=1;
    }
     else
    {
      a[j][m]=a[j-1][m-1]+a[j-1][m];
    }
   }
  }
  for (int i=0;i<length ;i++ )
  {
   for(int j=0;j<length-(i+1);j++)
    Console.Write(str1);
   for (int m=0;m<=i;m++)
    Console.Write("{0} ",a[i][m]);
   Console.WriteLine();
  }
  Console.ReadKey();
 }
}

判断一个数是否为素数。

using System;
class App
{
 public static void Main()
 {
  Console.WriteLine("@ 请输入一个数字,我们为你判断是否为素数 @");
  int i=1,n;
  n=Int32.Parse(Console.ReadLine());
  while (++i<n)
  {
   if (n%i==0)
   {
    Console.WriteLine("不是素数");
    break;  //用break终止循环
   }
   if (i==n)
   {
    Console.WriteLine("是素数");
   }
    
  }
 }

}

c#排序

using System;
class Class1
{
 static void Main(string[] args)
 {
  Console.WriteLine("please enter ten numbers separated with a comma:");
  string input=Console.ReadLine();
  int[] a=new int[10];
  int m;
  int commaPos1=input.IndexOf(',',0);
  a[0]=Convert.ToInt32(input.Substring(0,commaPos1));
  int commaPos2=input.IndexOf(',',commaPos1+1);
  a[1]=Convert.ToInt32(input.Substring(commaPos1+1,commaPos2-commaPos1-1));
  int commaPos3=input.IndexOf(',',commaPos2+1);
  a[2]=Convert.ToInt32(input.Substring(commaPos2+1,commaPos3-commaPos2-1));
  int commaPos4=input.IndexOf(',',commaPos3+1);
  a[3]=Convert.ToInt32(input.Substring(commaPos3+1,commaPos4-commaPos3-1));
  int commaPos5=input.IndexOf(',',commaPos4+1);
  a[4]=Convert.ToInt32(input.Substring(commaPos4+1,commaPos5-commaPos4-1));
  int commaPos6=input.IndexOf(',',commaPos5+1);
  a[5]=Convert.ToInt32(input.Substring(commaPos5+1,commaPos6-commaPos5-1));
  int commaPos7=input.IndexOf(',',commaPos6+1);
  a[6]=Convert.ToInt32(input.Substring(commaPos6+1,commaPos7-commaPos6-1));
  int commaPos8=input.IndexOf(',',commaPos7+1);
  a[7]=Convert.ToInt32(input.Substring(commaPos7+1,commaPos8-commaPos7-1));
  int commaPos9=input.IndexOf(',',commaPos8+1);
  a[8]=Convert.ToInt32(input.Substring(commaPos8+1,commaPos9-commaPos8-1));
  a[9]=Convert.ToInt32(input.Substring(commaPos9+1,input.Length-commaPos9-1));
  for (int i=0;i<10 ;i++ )
  {
   for (int j=i+1;j<10 ;j++ )
   {
    if (a[i]>a[j])
    {
     m=a[i];
     a[i]=a[j];
     a[j]=m;
    }
   }
  }
  Console.WriteLine("after the sort,the result as follows");
  for (int i=0;i<10 ;i++ )
  {
   Console.Write("{0} ",a[i]);
  }
  Console.ReadKey();
 }
}

输出10个数字 不重复 1到 10

using System;
using System.Collections;
using System.Threading;
class Program
{
 public static void Main()
 {
  while(true)
  {
  ArrayList al=new ArrayList();
  for (int i=1;i<=10 ;i++ )
  {
   al.Add(i);
  }
  Random r=new Random();
  for (int i=0;i<10 ;i++ )
  {
   int index=r.Next(0,al.Count);
   Console.Write("{0} ",al[index]);
   al.RemoveAt(index);

  }
     Thread.Sleep(1000);
  Console.WriteLine();

 }
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值