峰之博纹 - Pelephone个人博客

卡就一个asp.net

  好久没碰asp.net这个东西了,由于最近发昏。又装起了vs2008.想不到微软搞东西的速度还蛮快。vs2005都还没玩过这么快就08来了,这个庞然大物造成我电脑几次死马的现象。之前几次在博客里狂批.net的不好,但并不意为着就不用学了。呵呵,玩程序的有时就这么反常,虽然人人都说做这行没什么前途,但出于兴趣,又不想听到有人说自己不会.net这个东西。自己喜欢就行了,just do it!

  练习练习,放两段练习代码上来:
1.输入10个整数,并判断其中素数的算法。


 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EX101
7 {
8 class Program
9 {
10 private static List<int> InputData(){
11 int count = 10;
12 List<int> lst = new List<int>();
13 Console.WriteLine("请输入{0}组整数:n",count);
14 while (lst.Count<10)
15 {
16 string tmp=Console.ReadLine();
17 int n;
18 if(int.TryParse(tmp,out n)){
19 lst.Add(n);
20 }
21 }
22 return lst;
23 }
24
25 private static List<int> GetPrimes(List<int> data)
26 {
27 List<int> lstPrimes = new List<int>();
28 foreach (int number in data)
29 {
30 bool isPrime = true;
31 for (int divisor = 2; divisor <= number / 2; divisor++)
32 {
33 if (number % divisor == 0)
34 {
35 isPrime = false;
36 break;
37 }
38 }
39 if (isPrime)
40 {
41 lstPrimes.Add(number);
42 }
43 }
44 return lstPrimes;
45 }
46
47 static void Main(string[] args)
48 {
49 List<int> lstData = InputData();
50 List<int>lstPrimes = GetPrimes(lstData);
51
52 Console.WriteLine("您输入的数列为:");
53 foreach(int item in lstData){
54 Console.Write("{0},",item);
55 }
56
57 Console.WriteLine("其中的素数有:");
58 foreach(int item in lstPrimes){
59 Console.Write("{0},",item);
60 }
61
62 Console.ReadKey();
63
64
65 }
66 }
67 }





2.开创建文件。并向其写入数据:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EX102
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 Console.WriteLine("输入文件:");
13 string fileName = Console.ReadLine();
14
15 System.IO.FileStream fstream = new System.IO.FileStream
16
17 (fileName, System.IO.FileMode.OpenOrCreate);
18
19 System.IO.StreamWriter sw = new System.IO.StreamWriter(fstream);
20 Console.WriteLine("开始输入文件内容,如果输入exit则结束:");
21 do
22 {
23 string tmp = Console.ReadLine();
24 if(string.Compare(tmp,"exit",true) == 0)
25 {
26 break;
27 }
28 sw.WriteLine(tmp);
29 }while(true);
30
31 sw.Close();
32
33 Console.WriteLine("程序结束,按回车键退出。");
34 Console.ReadLine();
35 }
36 }
37 }







贴完cs文件舒服了。

posted @ 2008-04-09 15:46  Pelephone  阅读(440)  评论(0编辑  收藏  举报