鱼C论坛

 找回密码
 立即注册
查看: 2568|回复: 15

[技术交流] 【小正】C#系列随手练,写啥发啥

[复制链接]
发表于 2014-10-17 21:28:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码难度不限,长度不限,随手写啥发啥,代码不一定是最简练的,哦也~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-17 21:29:43 | 显示全部楼层
随手1:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication1
  6. {
  7.    
  8.     class Division
  9.     {
  10.         public float a;
  11.         public float b;
  12.     }

  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             const double PI=3.1415927;
  18.             Division x = new Division();
  19.             x.a = 1;
  20.             x.b = 3;

  21.             Console.WriteLine("a/b={0:F3}",x.a/x.b);
  22.             //Console.ReadLine();
  23.             Division y = x;
  24.             Console.WriteLine("y====>a/b={0:F3}", y.a / y.b);
  25.             Console.WriteLine("PI="+PI);

  26.             short c = 32766;
  27.             try
  28.             {
  29.                 checked { c += 2; };
  30.                 //checked { c += 1; };
  31.                 Console.WriteLine("c=" + c);
  32.             }
  33.             catch( OverflowException error)
  34.             {
  35.                 Console.WriteLine(error.Message);
  36.             }
  37.                         
  38.             

  39.             string s;
  40.             Console.WriteLine("Input s:");
  41.             s = Console.ReadLine();
  42.             Console.WriteLine("s={0}",s);
  43.             Console.ReadLine();
  44.         }
  45.     }
  46. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-17 21:30:21 | 显示全部楼层
随手2:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             short x = 32766;
  12.             ushort y = 65534;
  13.             x += 1;
  14.             y += 1;
  15.             Console.WriteLine("x={0}", x);
  16.             Console.WriteLine("y="+ y);

  17.             x += 1;
  18.             y += 1;
  19.             Console.WriteLine("x={0}", x);
  20.             Console.WriteLine("y=" + y);

  21.             bool a = true;
  22.             bool b = false;
  23.             Console.WriteLine("a={0}", a);
  24.             Console.WriteLine("b=" + b);
  25.             a = !b;
  26.             Console.WriteLine("a={0}", a);
  27.             float c = 3.1415926F;
  28.             double d = 3.14159265358979;
  29.             Console.WriteLine("c={0}", c);
  30.             Console.WriteLine("d={0}", d);

  31.             float e = 1.0F;
  32.             int f = 2;
  33.             Console.WriteLine("e/f=" + e / f);

  34.             Console.ReadLine();
  35.         }
  36.     }
  37. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-17 21:31:07 | 显示全部楼层
随手3:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication3
  6. {
  7.     class Test
  8.     {
  9.         public string ss = "hello,C#";
  10.         public static int count=66;
  11.       
  12.     }

  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             string tmp;
  18.             int rand = new Random().Next(100);
  19.             int guess = 0;
  20.             int count = 0;

  21.             Test tst1 = new Test();
  22.             Console.WriteLine(tst1.ss);
  23.             Test.count += 4;
  24.             Console.WriteLine(Test.count);
  25.      
  26.      
  27.             do
  28.             {
  29.                 Console.Write("Input a numbet:");
  30.                 try
  31.                 {
  32.                     tmp = Console.ReadLine();
  33.                     guess = Int32.Parse(tmp.Trim());
  34.                     if (guess > rand)
  35.                     {
  36.                         Console.WriteLine("BIG!");
  37.                     }

  38.                     else if (guess < rand)
  39.                     {
  40.                         Console.WriteLine("SMALL!");
  41.                     }

  42.                     else
  43.                     {
  44.                         Console.WriteLine("You`re Right!");
  45.                     }
  46.                 }
  47.                 catch (Exception err)
  48.                 {
  49.                     Console.WriteLine(err.Message);
  50.                 }
  51.             } while (guess != rand);
  52.             Console.ReadLine();
  53.         }      
  54.     //
  55.     }
  56. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-17 21:32:27 | 显示全部楼层
随手4:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication3
  6. {
  7.     class Weigth
  8.     {
  9.         public static float getweigth()
  10.         {
  11.             float weight;
  12.             Random rand1 = new Random();
  13.             weight = (float)rand1.NextDouble();

  14.             return weight;
  15.         }
  16.     }

  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.          
  22.          
  23.             int rand = new Random().Next(100);
  24.             Console.WriteLine("rand="+rand);

  25.             Weigth w1 = new Weigth();      
  26.             Console.WriteLine("weight="+Weigth.getweigth());

  27.             

  28.             Console.ReadLine();
  29.         }
  30.         //
  31.     }
  32. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-17 21:33:36 | 显示全部楼层
随手5:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication5
  6. {
  7.     class Re_Fun
  8.     {
  9.         public float mile;
  10.         public string road;
  11.         private string words="最初的字符串";
  12.         
  13.         //定义类的属性
  14.         public string Words
  15.         {
  16.             get
  17.             {
  18.                 return words ;
  19.             }
  20.             //set
  21.             //{
  22.              //   words = value;
  23.             //}
  24.         }

  25.         public void test()
  26.         {
  27.             Console.WriteLine("无参函数");
  28.         }

  29.         public void test(float Mile,string Road="星际航线")
  30.         {
  31.             mile = Mile;
  32.             road = Road;
  33.             Console.WriteLine(mile+road);
  34.         }
  35.     }
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             Re_Fun rf1 = new Re_Fun();
  41.             rf1.test();
  42.             rf1.test(5);
  43.             rf1.test(8.68F, "西北路");

  44.             //rf1.Words = "我给你赋值";
  45.             Console.WriteLine("words="+rf1.Words);
  46.             Console.ReadLine();
  47.         }
  48.     }
  49. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-17 21:34:30 | 显示全部楼层
@~风介~  明早要比赛了  所有队伍都来我们学校了 VNC云平台展示 哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-10-17 23:50:04 | 显示全部楼层
Python_GoWithMe 发表于 2014-10-17 21:34
@~风介~  明早要比赛了  所有队伍都来我们学校了 VNC云平台展示 哈哈

预祝成功!!!另外,什么比赛啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-18 12:18:12 | 显示全部楼层
~风介~ 发表于 2014-10-17 23:50
预祝成功!!!另外,什么比赛啊?

华北五省计算机
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-10-18 12:42:41 | 显示全部楼层

嘿嘿,我在南方
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-20 16:13:29 | 显示全部楼层
随手6:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication6
  6. {
  7.     class Car
  8.     {
  9.         public string color;
  10.         public double weigth;

  11.         public string Color
  12.         {
  13.             get
  14.             {
  15.                 return color;
  16.             }
  17.             set
  18.             {
  19.                 color = value;
  20.             }
  21.         }
  22.         public Car()
  23.         {
  24.             Color ="red_car";
  25.         }
  26.         public Car(string cor="innital color")
  27.         {
  28.             Color = cor;
  29.         }
  30.         public Car(string cor,double weig=5.5)
  31.         {
  32.             Color = cor;
  33.             weigth = weig;
  34.         }
  35.     }
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             int[] intArray = new int[10];
  41.             intArray[0] = 1;
  42.             intArray[2] = (int)3.45;
  43.             for (int i = 0; i < intArray.Length; i++)
  44.             {
  45.                 Console.WriteLine("intArray["+i+"]="+intArray[i]);
  46.             }
  47.             //
  48.             Car[] carArray=new Car[10];
  49.             for (int i = 0; i < carArray.Length; i++)
  50.             {
  51.                 carArray[i] = new Car();
  52.             }
  53.                 carArray[0].Color = "blue_car";
  54.                 carArray[1] = new Car("golden_car");
  55.                 carArray[2] = new Car("nb_car",8.889);
  56.                 Console.WriteLine("carArray[2]=" +carArray[2].Color+"----" +carArray[2].weigth);
  57.             //
  58.                 List<string> carLst = new List<string>();
  59.                 carLst.Add(carArray[2].color);
  60.                 carLst.Add(carArray[2].Color);
  61.             foreach(string var in carLst)
  62.             {
  63.                 Console.WriteLine("carlst="+var);
  64.             }
  65.             for (int i = 0; i < carArray.Length; i++)
  66.             {
  67.                 Console.WriteLine("carArray[" + i + "]=" + carArray[i].Color);
  68.             }

  69.             Console.ReadLine();
  70.         }
  71.     }
  72. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-20 16:14:31 | 显示全部楼层
随手7:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication7
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[, ,] intArray3 = new int[3,4,5];
  12.             int[,] intArray2;
  13.             intArray2=new int[2,3];
  14.             intArray2[0,0]=23;
  15.             int k=1;
  16.             Console.WriteLine(intArray2[0,0]);
  17.             for (int i = 0; i < intArray2.GetLength(0); ++i)
  18.             {
  19.                 for (int j = 0; i < intArray2.GetLength(1); ++j)
  20.                 {
  21.                     //intArray2[i, j] = ++k;
  22.                     intArray2[1,2] = 9989;
  23.                 }
  24.             }

  25.             
  26.             foreach (int l in intArray2)
  27.             {
  28.                 Console.WriteLine("{0,5}",l);
  29.             }
  30.            
  31.                 Console.ReadLine();
  32.         }
  33.     }
  34. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-20 16:16:55 | 显示全部楼层
随手8:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace ConsoleApplication8
  6. {
  7.     public  class  Bus
  8.     {
  9.         public  void bus()
  10.         {
  11.             Console.WriteLine("BUS类函数");
  12.         }
  13.     }
  14.     public class Car:Bus
  15.     {
  16.         string[] wheels = new string[2];

  17.         public  void bus()
  18.         {
  19.             Console.WriteLine("CAR类函数");
  20.         }
  21.         

  22.         public Car()
  23.         {
  24.             wheels[0] = "轮子0";
  25.             wheels[1] = "轮子1";
  26.         }
  27.         public string this[int index]
  28.         {
  29.             get
  30.             {
  31.                 return wheels[index];
  32.             }
  33.             set
  34.             {
  35.                 wheels[index] = value;
  36.             }
  37.         }
  38.     }
  39.     class Program
  40.     {
  41.         static void Main(string[] args)
  42.         {
  43.             Car car1 = new Car();
  44.             Console.WriteLine(car1[0]);
  45.             car1[1]="1---1";
  46.             Console.WriteLine(car1[1]);
  47.             car1.bus();
  48.             Console.ReadLine();
  49.         }
  50.     }
  51. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-20 16:18:19 | 显示全部楼层
随手9:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Windows.Forms;

  7. namespace ConsoleApplication9
  8. {
  9.     class FromWin:System.Windows.Forms.Form
  10.     {
  11.         public FromWin()
  12.         {
  13.             Height = 200;
  14.             Width = 500;
  15.             Text = "windows窗体-鱼C加油-001";
  16.             Button bt1=new Button ();
  17.             bt1.Height = 50;
  18.         }
  19.     }
  20.     class Program

  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             FromWin fr = new FromWin();
  25.             Application.Run(fr);
  26.         }
  27.     }
  28. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-10-20 16:18:54 | 显示全部楼层
@小甲鱼  为什么没有C#板块内容嘞?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-10-23 11:46:23 | 显示全部楼层
Python_GoWithMe 发表于 2014-10-20 16:18
@小甲鱼  为什么没有C#板块内容嘞?

也是有的,在综合交流区:http://bbs.fishc.com/forum-43-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-15 10:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表