1. C#基础
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
/*
Console.WriteLine("hello !");
string s=Console.ReadLine();//用户输入文字时,程序是暂停的,回车后把输入的作为返值,并存放到s当中
Console.WriteLine(s+"你好");//输出用户输入的
int i1 = 10;
int i2 = 20;
//Console.WriteLine(i1+"+"+i2+"="+(i1+i2));//方法一
Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2,"aaa");//方法二
//使用占位符,{0}{1}{2}分别表示三个占位符,顺序从0开始
Console.WriteLine("请输入男孩的名字");
string boy = Console.ReadLine();
Console.WriteLine("请输入女孩的名字");
string girl = Console.ReadLine();
Console.WriteLine("{0}爱{1}",boy,girl);
*/
/*
string s1 = "\"ab\"";
Console.WriteLine(s1);
string s2 = "a\nb";
Console.WriteLine(s2);
string s3 = @"c:\temp\user\new\1.jpg";//@表示字符串中的\不当成转义符,@只针对转义符
Console.WriteLine(s3);
*/
Console.WriteLine("请输入第一个数字");
string s4 = Console.ReadLine();
int i1 = Convert.ToInt32(s4);
Console.WriteLine("请输入第二个数字");
//string s5 = Console.ReadLine();
//int i2 = Convert.ToInt32(s5);
int i2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2);
//中文做变量名
string 姓名 = Console.ReadLine();
Console.WriteLine(姓名+" 你好");
人 人1 = new 人();
人1.年龄 = 20;
人1.说话();
Console.ReadKey();//按下一个按键继续执行
}
}
}
///////////////////////////////////////////////////////////////
//人.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class 人
{
public int 年龄 { get; set; }
public void 说话()
{
Console.WriteLine(年龄+" 说话");
}
}
}
2. 字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cast入门
{
class Program
{
static void Main(string[] args)
{
//类型转换
Console.WriteLine("int最大值{0},最小值{1}",int.MaxValue,int.MinValue);
Console.WriteLine("byte最大值{0},最小值{1}",byte.MaxValue,byte.MinValue);
byte b = 3;
int i1 = b;
//b = i1;
b = (byte)i1;
string s = "123";
int i2 = Convert.ToInt32(s);
Console.WriteLine(i2);
Console.ReadKey();
}
}
}
////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 字符串
{
class Program
{
static void Main(string[] args)
{
/*
char c1='a';
char c2='b';
string s1 = "hi";
string s2 = "a";
string s3 = "";
string s4 = "hello";
Console.WriteLine(s4.Length);//5, 不是6,和C语言不同不考虑'\0'
Console.WriteLine(s4[1]);
char c3=s4[3];
for (int i = 0; i < s4.Length; i++)
{
char c6=s4[i];
Console.WriteLine(c6);
}
for (int i = s4.Length - 1; i >= 0; i--)
{
Console.WriteLine(s4[i]);
}
//s4[0]='y';//不能赋值
*/
/*
string s1 = "hello";
char[] chars = s1.ToCharArray();//将字符串转换成字符数组
chars[0]='a';
string s2 = new string(chars);
Console.WriteLine(s1);
Console.WriteLine(s2);
*/
string s1 = "hello";
string s10=s1;
char[] chars = s1.ToCharArray();
chars[0]='a';
s1 = new string(chars);
Console.WriteLine(s1);
Console.WriteLine(s10);
Console.ReadKey();
}
}
}
3. 枚举学习
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 枚举学习
{
enum Gender { Male,Female,Unknown};//声明枚举
class Program
{
static void Main(string[] args)
{
Gender g = Gender.Female;//枚举的意义在于限定变量的取值范围
//Gender g1 = Gender.Male;
/*
string s = "Male";
if (s == "Male")
{
Console.WriteLine("女");
}
else if (s == "Unknown")
{
Console.WriteLine("未知");
}
esle
{
Console.WriteLine("非法输入!");
}
*/
}
}
}
4. 条件判断
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 条件判断
{
class Program
{
static void Main(string[] args)
{
/*
string s1 = Console.ReadLine();
int age = Convert.ToInt32(s1);
if (age > 20)
{
Console.WriteLine("成年人");
}
//else
//{
// Console.WriteLine("小屁孩");
//}
else if (age > 10)
{
Console.WriteLine("儿童");
}
else
{
Console.WriteLine("婴幼儿");
}
*/
/*
Console.WriteLine("请输入密码");
string s1 = Console.ReadLine();
if (s1 == "888888")
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误,请再输入一次");
s1 = Console.ReadLine();
if (s1 == "888888")
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误");
}
}
*/
/*
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
if (username == "admin" && password == "888888")
{
Console.WriteLine("正确");
}
else
{
Console.WriteLine("用户名、密码错误");
if (username != "admin")
{
Console.WriteLine("用户名不存在");
}
}
*/
Console.WriteLine("请输入年龄");
string strAge = Console.ReadLine();
int age = Convert.ToInt32(strAge);
if (age > 18)
{
Console.WriteLine("可以查看!");
}
else if (age < 10)
{
Console.WriteLine("不允许查看!");
}
else
{
Console.WriteLine("是否继续查看(yes、no)?");
string isContinue=Console.ReadLine();
if (isContinue == "yes")
{
Console.WriteLine("看吧!");
}
else if (isContinue == "no")
{
Console.WriteLine("乖!");
}
else
{
Console.WriteLine("输入错误");
}
}
Console.ReadKey();
}
}
}
5. 数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 条件判断
{
class Program
{
static void Main(string[] args)
{
/*
string s1 = Console.ReadLine();
int age = Convert.ToInt32(s1);
if (age > 20)
{
Console.WriteLine("成年人");
}
//else
//{
// Console.WriteLine("小屁孩");
//}
else if (age > 10)
{
Console.WriteLine("儿童");
}
else
{
Console.WriteLine("婴幼儿");
}
*/
/*
Console.WriteLine("请输入密码");
string s1 = Console.ReadLine();
if (s1 == "888888")
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误,请再输入一次");
s1 = Console.ReadLine();
if (s1 == "888888")
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误");
}
}
*/
/*
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine();
if (username == "admin" && password == "888888")
{
Console.WriteLine("正确");
}
else
{
Console.WriteLine("用户名、密码错误");
if (username != "admin")
{
Console.WriteLine("用户名不存在");
}
}
*/
Console.WriteLine("请输入年龄");
string strAge = Console.ReadLine();
int age = Convert.ToInt32(strAge);
if (age > 18)
{
Console.WriteLine("可以查看!");
}
else if (age < 10)
{
Console.WriteLine("不允许查看!");
}
else
{
Console.WriteLine("是否继续查看(yes、no)?");
string isContinue=Console.ReadLine();
if (isContinue == "yes")
{
Console.WriteLine("看吧!");
}
else if (isContinue == "no")
{
Console.WriteLine("乖!");
}
else
{
Console.WriteLine("输入错误");
}
}
Console.ReadKey();
}
}
}
6. 变量命名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 变量命名
{
class Program
{
static void Main(string[] args)
{
/*
int i1;
int i2,i3;
int i4 = 8;
i4++;
Console.WriteLine(i4);
*/
/*
int i=10;
i = 1 + i;
i += 2;
Console.WriteLine(i);
int x = 10;
int y ;
y = x + 80;
Console.WriteLine(y);
*/
/*
//交换两变量的值
int i1 = 10;
int i2 = 20;
Console.WriteLine("i1={0},i2={1}",i1,i2);
int i3;
i3 = i1;
i1 = i2;
i2 = i3;
Console.WriteLine("i1={0},i2={1}",i1,i2);
*/
//布尔运算
int i = 30;
bool b=(i==1);
Console.WriteLine("{0}\nb:{1}",i==1,b);
Console.WriteLine("i:{0}",i=1);
Console.WriteLine("{0}",i!=30);
int i1 = 20;
int i2 = 30;
Console.WriteLine("{0}",i1>10&&i2<100);
Console.WriteLine("{0}",i1>100&&i2<100);
Console.WriteLine("{0}",i1>200||i2<100);
Console.ReadKey();
}
}
}
7. 函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 函数
{
class Program
{
static void Main(string[] args)
{
int i = ReadInt();
i++;
Console.WriteLine(i);
Console.WriteLine(Max(30,50));
string[] names = { "tom","jerry","lucy"};
string s = Join(names,"|");
Console.WriteLine(s);
Console.ReadKey();
}
/*
static int ReadInt()
{
string s = Console.ReadLine();
int i = Convert.ToInt32(s);
return;
}
*/
static int CT(string s)
{
if (s == "tom")
{
return 1;
}
if (s == "jerry")
{
return 0;
}
return -1;
}
static void SayHello()
{
int i = 3;
if (i > 10)
{
return;
}
Console.WriteLine("哈喽哇!");
}
static int ReadInt()
{
string s = Console.ReadLine();
return Convert.ToInt32(s);
}
static int Max(int i1, int i2)
{
if (i1 > i2)
{
return i1;
}
return i2;
}
static int Sum(int[] values)
{
int sum = 0;
foreach (int i in values)
{
sum = sum + i;
}
return sum;
}
static string Join(string[] strs, string seperator)
{
string result = "";
for (int i = 0; i < strs.Length - 1; i++)
{
result = result + strs[i] + seperator;
}
if (strs.Length > 0)
{
result = result + strs[strs.Length - 1];
}
return result;
}
}
}
///////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 函数2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0}{1}{2}{3}{4}",333,55,66,77,88,99,10,"aaa");
string[] values = { "aaa","bbb","ccc"};
VFunc("aaa","111","222","33233","fasfsa","aaa");
SayHello("tom","张大虎","狗蛋");
Console.ReadKey();
}
static void VFunc(params string[] values)//加params说明这个函数参数个数可变
{
foreach (string s in values)
{
Console.WriteLine(s);
}
}
static void SayHello(string name, params string[] nicknames)
{
Console.WriteLine("我的名字是{0}",name);
foreach(string nickname in nicknames)
{
Console.WriteLine("我的昵称:{0}",nickname);
}
}
}
}
8. 函数重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 函数重载
{
class Program
{
static void Main(string[] args)
{
SayHello("王大壮");
SayHello("tom");
SayHello(20);
//string s = SayHello("jerry");
SayHello("tom","littletom");
Console.ReadKey();
}
static void SayHello(string name)
{
Console.WriteLine("大家好,我是:{0}",name);
}
/*
static void SayHello(string name)
{
Console.WriteLine("Hello,I am {0}",name);
}
*/
static void SayHello(int age)
{
Console.WriteLine("我{0}岁了",age);
}
/*
static string SayHello(string name)
{
return "大家好,我是"+name;
}
*/
static void SayHello(string name, string nickname)
{
Console.WriteLine("我的名字是{0},我的昵称是{1}",name,nickname);
}
}
}
9. For例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace For例子
{
class Program
{
static void Main(string[] args)
{
int i = 0;
for (i = 10; i < 20; i++)
{
Console.WriteLine(i);
}
//Console.ReadKey();
for ( i = 10; i > 0; i--)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
10. While语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace While语句
{
class Program
{
static void Main(string[] args)
{
//while (true)
//{
// Console.WriteLine("我在运行!");
//}
//int i = 0;
//while (i < 10)
//{
// i++;
// Console.WriteLine(i);
//}
//int i = 1;
//int sum = 0;
//while (i <= 100)
//{
// sum = sum + i;
// i++;
//}
//Console.WriteLine("{0}",sum);
//Console.WriteLine("请输入用户名:");
//string username = Console.ReadLine();
//Console.WriteLine("请输入密码:");
//string password = Console.ReadLine();
//while (username != "admin" || password != "888888")
//{
// Console.WriteLine("用户名、密码错误!");
// Console.WriteLine("请重新输入用户名");
// username = Console.ReadLine();
// Console.WriteLine("请输入密码");
// password = Console.ReadLine();
//}
//Console.WriteLine("请输入一个数字");
//string s = Console.ReadLine();
//while (s != "q")
//{
// int number = Convert.ToInt32(s);
// Console.WriteLine("{0}",number*2);
//}
//while (true)
//{
// Console.WriteLine("请输入一个数字");
// string s = Console.ReadLine();
// if (s == "q")
// return;
// int number = Convert.ToInt32(s);
// Console.WriteLine("{0}",number*2);
//}
//int max = 0;
//while (true)
//{
// Console.WriteLine("请输入一个数字(结束请输入end):");
// string s = Console.ReadLine();
// if (s == "end")
// {
// Console.ReadLine();
// return;
// }
// int number = Convert.ToInt32(s);
// if (number > max)
// {
// max = number;
// }
//}
//int i = 0;
//while (i < 12)
//{
// Console.WriteLine("i={0}",i);
// i++;
// if (i == 10)
// {
// return;
// }
// Console.WriteLine("自增以后i={0}",i);
//}
//Console.WriteLine("before ReadKey");
int sum = 0;
int i = 1;
while (i <= 100)
{
if ((i % 7) == 0)
{
i++;
Console.WriteLine(i-1);
continue;
}
sum = sum + i;
i++;
}
Console.WriteLine("{0}",sum);
Console.ReadKey();
}
}
}
11. Switch语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace While语句
{
class Program
{
static void Main(string[] args)
{
//while (true)
//{
// Console.WriteLine("我在运行!");
//}
//int i = 0;
//while (i < 10)
//{
// i++;
// Console.WriteLine(i);
//}
//int i = 1;
//int sum = 0;
//while (i <= 100)
//{
// sum = sum + i;
// i++;
//}
//Console.WriteLine("{0}",sum);
//Console.WriteLine("请输入用户名:");
//string username = Console.ReadLine();
//Console.WriteLine("请输入密码:");
//string password = Console.ReadLine();
//while (username != "admin" || password != "888888")
//{
// Console.WriteLine("用户名、密码错误!");
// Console.WriteLine("请重新输入用户名");
// username = Console.ReadLine();
// Console.WriteLine("请输入密码");
// password = Console.ReadLine();
//}
//Console.WriteLine("请输入一个数字");
//string s = Console.ReadLine();
//while (s != "q")
//{
// int number = Convert.ToInt32(s);
// Console.WriteLine("{0}",number*2);
//}
//while (true)
//{
// Console.WriteLine("请输入一个数字");
// string s = Console.ReadLine();
// if (s == "q")
// return;
// int number = Convert.ToInt32(s);
// Console.WriteLine("{0}",number*2);
//}
//int max = 0;
//while (true)
//{
// Console.WriteLine("请输入一个数字(结束请输入end):");
// string s = Console.ReadLine();
// if (s == "end")
// {
// Console.ReadLine();
// return;
// }
// int number = Convert.ToInt32(s);
// if (number > max)
// {
// max = number;
// }
//}
//int i = 0;
//while (i < 12)
//{
// Console.WriteLine("i={0}",i);
// i++;
// if (i == 10)
// {
// return;
// }
// Console.WriteLine("自增以后i={0}",i);
//}
//Console.WriteLine("before ReadKey");
int sum = 0;
int i = 1;
while (i <= 100)
{
if ((i % 7) == 0)
{
i++;
Console.WriteLine(i-1);
continue;
}
sum = sum + i;
i++;
}
Console.WriteLine("{0}",sum);
Console.ReadKey();
}
}
}
12. refout参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace refout参数
{
class Program
{
static void Main(string[] args)
{
int age = 20,age1=20;
IncAge(age);
IncAge2(out age);
IncAge(ref age1);
Console.WriteLine("无ref:\t"+age);
Console.WriteLine("加了ref:\t"+age1);
int i1 = 10;
int i2 = 20;
Console.WriteLine("交换前:\ni1:{0},i2{1}",i1,i2);
Swap(ref i1,ref i2);
Console.WriteLine("交换后:\ni1={0},i2={1}",i1,i2);
Console.ReadKey();
}
static void IncAge(int age)//IncAge内部改变的是副本
{
age++;
}
static void IncAge(ref int age)
{
age++;
}
static void IncAge2(out int age)
{
age=30;
Console.WriteLine("加了out的调用:"+age);
}
static void Swap(ref int i1,ref int i2)
{
int temp=i1;
i1=i2;
i2=temp;
}
}
}
13. 字符串练习1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 字符串练习1
{
class Program
{
static void Main(string[] args)
{
/*
//接收用户输入的字符串,将其反序输出
string s = Console.ReadLine();
for (int i = s.Length-1; i >= 0; i--)
{
Console.Write(s[i]);
}
*/
/*
//将一句话中的单词反序输出
string s1 = Console.ReadLine();
string[] words = s1.Split(' ');
for (int i = words.Length - 1; i >= 0; i--)
{
Console.Write(words[i]+" ");
}
Console.WriteLine("\n-------------");
//从email中提取用户名与域名
string email = Console.ReadLine();
int atIndex = email.IndexOf('@');
string username = email.Substring(0,atIndex);
string 域名 = email.Substring(atIndex+1);
Console.WriteLine(username);
Console.WriteLine(域名);
*/
/*
//提出文件中的文章标题与作者
string[] lines = System.IO.File.ReadAllLines(@"..\..\2.txt", Encoding.Default);
foreach (string line in lines)
{
//string[] strs = line.Split('');
string[] strs = line.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
string title=strs[0];//标题
string author=strs[1];//作者
title=title.Substring(0,Math.Min(17,title.Length));//截取标题的前17个字符
//Math.Min用来计算两个数中的最小值
title = title + "...";
Console.WriteLine("{0}|{1}",title,author);
}
*/
string value = GetConfigValue(@"..\..\3.ini","端口");
Console.WriteLine(value);
Console.ReadKey();
}
static string GetConfigValue(string filename, string itemName)
{
string[] lines = System.IO.File.ReadAllLines(filename,Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split('=');
string name=strs[0];
string value=strs[1];
if (name.Trim() == itemName)
{
return value.Trim();//Trim函数去除空格
}
}
return "错误";
}
}
}
14. 字符串学习1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 字符串学习1
{
class Program
{
static void Main(string[] args)
{
/*
String s = "Hello";
string s1=s.ToLower();
Console.WriteLine(s);
Console.WriteLine(s1);
s = s.ToLower();//并不是改变了字符串的内容,而是生成一个新的全部变成小写的字符串
Console.WriteLine(s);
Console.WriteLine(s.ToUpper());
string s2=" a b ";
s2 = s2.Trim();//去掉两边的空格
Console.WriteLine("|{0}|",s2);
*/
//bool b=("abc"=="ABC");
bool b = "abc".Equals("ABC",StringComparison.OrdinalIgnoreCase);
//Ignore:忽略,Case:大小写
Console.WriteLine(b);
//字符串比较
int i = "abc".CompareTo("123");//字符串比较
Console.WriteLine(i);
//字符串分隔
string s1 = "aaa,bbb,ccc,fadsfas.hello|f123,yes";
string[] strs = s1.Split(',','.','|');//以逗号等多个分隔符分隔字符串
foreach (string item in strs)
{
Console.WriteLine(item);
}
Console.WriteLine("------------");
string s2 = "aaa,bb,cc,12,3";
string[] strs2 = s2.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
foreach (string item in strs2)
{
Console.WriteLine(item);
}
Console.WriteLine("------------");
string s3 = "我是杰克逊我是麦克杰克逊";
string[] strs3 = s3.Split(new string[]{"我是"},StringSplitOptions.RemoveEmptyEntries);
foreach (string item in strs3)
{
Console.WriteLine(item);
}
//读文件
Console.WriteLine("------------");
string[] lines = System.IO.File.ReadAllLines(@"..\..\1.txt",Encoding.Default);
foreach (string item in lines)
{
//Console.WriteLine(item);
string[] strs4 = item.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(item+"\t"+strs4[2]);
}
Console.ReadKey();
}
}
}
15. 字符串学习2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 字符串学习2
{
class Program
{
static void Main(string[] args)
{
string s = "李时珍同志是一个好同志,是一个好医生,向李时珍学习";
s = s.Replace("李时珍","李素丽");//替换字符串
Console.WriteLine(s);
string s1 = "http://www.baidu.com";
string 域名 = s1.Substring(7);//取从第7个字符串开始的字字符串
Console.WriteLine(域名);
string s2 = s1.Substring(7,5);//从第7个位置截取5个字符
Console.WriteLine(s2);
string s3="我们的社会真和谐!";
if(s3.Contains("社会")||s3.Contains("和谐"))
{
Console.WriteLine("含有敏感词汇,请文明用语!");
}
if (s1.StartsWith("http://") || s1.StartsWith("https://"))
{
Console.WriteLine("是网址!");
}
else
{
Console.WriteLine("不是网址!");
}
string s4 = "你好,我是王某某";
int i = s4.IndexOf("我是");
int i2 = s4.IndexOf("你是");
Console.WriteLine(i);
Console.WriteLine(i2);
Console.ReadKey();
}
}
}
没有评论:
发表评论