using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 练习
{
class Program
{
static void Main(string[] args)
{
//第一个练习
/*
int sum = 0;
int i;
for(i=1;i<101;i++)
{
sum = sum + i;
}
Console.WriteLine("{0}",sum);
*/
//练习2
/*
string s = Console.ReadLine();
Console.WriteLine("{0}", s+"我爱你");
int i1 = 100;
int i2 = 300;
Console.WriteLine(i1+"+"+i2+"="+(i1+i2));
int s1 = 200;
int s2 = 300;
Console.WriteLine("{0}+{1}+{2}",s1,s2,s2+s1);
Console.WriteLine("请输入男孩的名字");
string boy = Console.ReadLine();
Console.WriteLine("请输入女孩的名字");
string girl = Console.ReadLine();
Console.WriteLine("{0}爱{1}",boy,girl);
//int size = "3";看起来像数字,其实是字符串
// boy = 3;类型一旦确定就不能赋值其他类型。
*/
//练习3
/*
string i = "ab";
Console.WriteLine("{0}",i);// 打印ab,如何打印“ab”呢,看入下
string i1="\"ab\"";
Console.WriteLine("{0}",i1);//使用转义符\将“转义使他不被电脑认为是字符串结束,而是普通字符"a\nb"输出回车换行,想输出转义符\怎么办呢如下。
string i2 = "a\\b";
Console.WriteLine("{0}",i2);//双\\就可输出\,但是如果输出长路径,此方法就太麻烦,那么用什么方法解决呢,如下
string i3=@"c\d\c\c\c\c\c\c\c";//@表示字符串中的\不当成转义符,这样就可以很方便的输出路径,注意@只针对转义符有意义
Console.WriteLine(i3);
Console.WriteLine("请输入第一个数字");
string i4 = Console.ReadLine();
int s1 = Convert.ToInt32(i4);
Console.WriteLine("请输入第二个数字");
int s2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}+{1}={2}",s1,s2,s2+s1);
int a1 = 10;
int a2 = 20;
Console.WriteLine("a1={0},a2={1}",a1,a2);
int zhuan = 0;
zhuan = a1;
a1 = a2;
a2 = zhuan;
Console.WriteLine("a1={0},a2={1}",a1,a2);
*/
//联系4
/*
int i = 30;
bool q=(i==1);
Console.WriteLine("{0},{1},{2},{3}",i,i==1,q,i=2);
int age = Convert.ToInt32(Console.ReadLine());
if (age > 20)
{
Console.WriteLine("成年人");
}
else
{
Console.WriteLine("小屁孩");
}
int age1 = Convert.ToInt32(Console.ReadLine());
if(age1>50)
{
Console.WriteLine("old man");
}
else if (age1 > 20)
{
Console.WriteLine("young man");
}
else
{
Console.WriteLine("child");
}
Console.WriteLine("请输入密码:");
int password = Convert.ToInt32(Console.ReadLine());
if(password==88888)
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误!");
}
*/
//练习5
/*
Console.WriteLine("请用户输入密码");
int password = Convert.ToInt32(Console.ReadLine());
if (password == 88)
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码不对,请重新输入:");
int mima = Convert.ToInt32(Console.ReadLine());
if (mima == 88)
{
Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误!");
}
}
*/
//练习6
/*
Console.WriteLine("请输入用户名:");
string name = Console.ReadLine();
Console.WriteLine("请输入密码:");
int password = Convert.ToInt32( Console.ReadLine());
if(name=="admin"&&password==1)
{
Console.WriteLine("正确");
}
else
{ Console.WriteLine("错误");
if (name != "admin")
{
Console.WriteLine("用户名不存在");
}
}
*/
//练习7
/*
Console.WriteLine("请输入用户年龄:");
int age = Convert.ToInt32(Console.ReadLine());
if(age>17)
{
Console.WriteLine("可以查看");
}
else if(age<10)
{
Console.WriteLine("不可以查看");
}
else if(age > 9)
{
Console.WriteLine("是否继续查看,继续输入yes,退出输入no");
string pandu = Console.ReadLine();
if (pandu == "yes")
{
Console.WriteLine("查看成功");
}
else if(pandu=="no")
{
Console.WriteLine("小孩子,不可以看");
}
}
*/
//练习8
/*
Console.WriteLine("请输入i1:");
int i1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入i2");
int i2 = Convert.ToInt32(Console.ReadLine());
if(i1>0&&i2>0)
{
i1++;
Console.WriteLine("{0}", i1 + i2);
}
else if(i1<0&&i2<0)
{
i1 = i1 - 10;
Console.WriteLine("{0}",(i1*i2));
}
*/
//练习9
/*
int i = 10;
switch (i)
{
case 3://只能写常量,不能写变量
Console.WriteLine("3");
break;
case 10://唯一一个case后不用加break的
case 20://相当于if(i=10||i=20)
Console.WriteLine("整钱");
Console.WriteLine("你真有钱");
break;
default://相当于if语句的else
Console.WriteLine("你输入的{0}没有意义!",i);
break;
}
*/
//练习10
/*
int i = 0;
int sum = 0;
while (i < 100)
{
i++;
sum = sum + i;
}
Console.WriteLine(sum);
Console.WriteLine("请用户输入用户名");
string uername = Console.ReadLine();
Console.WriteLine("请用户输入密码");
string password = Console.ReadLine();
while(uername!="admin"&&password!="8")
{
Console.WriteLine("重新输入");
}
*/
//练习11
/*
string front;
do
{
Console.WriteLine("请输入数字");
front = Console.ReadLine();
while (front != "q")
{
Console.WriteLine("这个数字{0}的二倍是{1}", front, Convert.ToInt32(front) * 2);
break;
}
} while (front != "q");
return;
*/
//练习12
/*
Console.WriteLine("请输入数");
string shu = Console.ReadLine();
while (shu != "q")//当是q时退出程序
{
int num = Convert.ToInt32(shu);
Console.WriteLine(num*2);
Console.WriteLine("请输入数");
shu = Console.ReadLine();//重新输入的数进行在循环判断
}
*/
//练习13
/*
int max = 0;
while(true)
{
Console.WriteLine("请输入一个数,当输end时退出程序:");
string s = Console.ReadLine();
if(s=="end")
{
Console.WriteLine("最大数是{0}",max);
Console.ReadKey();
return;
}
int number = Convert.ToInt32(s);
if(number>max)//新的数和max进行比较诞生更大的数,赋给max,从而产生最大数max
{
max = number;
}
}
*/
//练习14 用while实现从1加到100,能被7整除的数除外
/*
int i=0;
int sum = 0;
while(i<=100)
{
if((i%7)==0)
{
i++;
continue;
}
sum= sum + i;
i++;
}
Console.WriteLine("{0}",sum);
*/
//练习15机器人
/*
Console.WriteLine("大家好,我是机器人");
int full = 10;
while(true)
{
Console.WriteLine("你想对我说什么呢?");
string str = Console.ReadLine();
if(full<=0)
{
Console.WriteLine("没力气了请喂我几个食物:");
string food = Console.ReadLine();
int food1 = Convert.ToInt32(food);
if(food1<=0)
{
Console.WriteLine("你什么也没给生气了不聊了");
return;
}
else if(food1>0)
{
full = full + food1;
Console.WriteLine("谢谢你的食物可以继续聊了");
}
continue;
}
if(str=="今天天气怎么样")
{
Console.WriteLine("很好啊!");
}
else if(str=="最近过的怎么样啊")
{
Console.WriteLine("不大好");
}
else if (str == "你喜欢我吗")
{
Console.WriteLine("请别迷恋锅,锅只是个传说");
}
else
{
Console.WriteLine("你说的什么语言啊,你是火星来的把");
}
full--;
}
*/
//练习16
/*
Console.WriteLine("int的最大值是{0}int的最小值是{1}",int.MaxValue,int.MinValue);
int b;
byte c=1;
b = c;//小数据类型可以向大数据转换,隐试转换
c = (byte)b;//大类型向小类型数据转换必须是强制转换,出了问题自己负责,例如如下
int f = 2000;
byte s;//最大 值255
s=(byte)f;//注意此方法只能用于相同类型数据,不同类型需用convert进行转换
Console.WriteLine(s);//原数据是2000,强制转换后输出的是208,这就是溢出
int i;
int sum = 0;
for (i = 0; i <= 100;i++ )
{
sum = sum + i;
}
Console.WriteLine("从一到100的和是{0}",sum);
*/
//练习17
/*
enum gender{male,female};
class pp
{
gender g=gender.female;//枚举的好处就是限定了变量的取值范围,程序用起来更方便。
}
*/
//练习18
/*
int[] value={1,2,3};//类型后面跟[],就是这个类型的数组。
Console.WriteLine(value.Length);//元素个数
Console.WriteLine(value[0]);//第0个数
int max = 0;
for (int i = 0; i < value.Length;i++ )
{
if(max<value[i])
{
max=value[i];
}
}
Console.WriteLine(max);
*/
//练习18 数组练习
/*
string[] names = { "乔洋","石头","花生"};
string s = "";
if (names.Length > 0)
{
s = s + names[0];
}
for (int i = 1; i < names.Length;i++ )
{
s=s+"|"+names[i];
}
Console.Write("{0}",s);
*/
//练习19
/*
string[] s = { "生","死","由","命","吧"};
int i;
for ( i = 0; i < s.Length;i++ )
{
Console.Write("{0}",s[i]);
}
string zhongjian = "";
for (i = 0; i <s.Length/2;i++ )//整数中运算5/2=2,因为是整除
{
zhongjian = s[i];
s[i]=s[s.Length-i-1];
s[s.Length-i-1]=zhongjian;
}
for (i = 0; i < s.Length;i++ )
{
Console.Write("{0}",s[i]);
}
*/
//练习20
/*
string[] names = { "曹操","刘备","张飞","关羽","赵子龙","鲁班"};
foreach(string name in names)//将数组的没一个元素放到name变量中,进行遍历
{
Console.Write("{0}",name);
}
*/
//练习21
/*
int[] shuzi = { 100,100,100,1000,100,100};
int sum = Sum(shuzi);
Console.WriteLine(sum);
*/
//练习22
/*
string[] name = {"圣诞节","山东","是的","司法" };
string hebing = Join(name,"|");
Console.WriteLine(hebing);
*/
//练习23
/*
SayHello("刘娇贤","改革","开放","30年");
*/
//;练习24 函数重载:相同的函数名相同,参数个数或者类型不同可构成重载,和返回值类型没有关系。
/*
Console.WriteLine("你爱谁啊?");
Love();
Love("小芳");
string s =Love("妹妹","18");
Console.WriteLine(s);
*/
//练习25 变量的名和变量指向值的区别
/*
string s1 = "hello";
char[] chars = s1.ToCharArray();//只是拷贝
chars[0]='d';
string s10 = s1;
s1 = new string(chars);//s1又指向了新的变量,而内存中的字符串hello不是被dello覆盖,而是指向其他变量的值
Console.WriteLine(s1);
Console.WriteLine(s1);//指向变量变了,但s1指向在内存中的字符串没变
Console.WriteLine(s10);//s10指向s1指向的内存字符串,还没变说明以上结论正确。这就是字符串的不可变性。
*/
//练习26
/*
string s = "hello";
string s1 = s.ToUpper();//将字符串化为大写
Console.WriteLine(s1);
Console.WriteLine(s1.ToLower());//将字符串化为小写
string a = " dssssss ";
Console.WriteLine("|{0}|",a.Trim());//去掉字符串两边的空格
string h = "abc";
bool p=h.Equals("ABC",StringComparison.OrdinalIgnoreCase);//忽略大小写,2个字符串进行比较,相等返回true
Console.WriteLine(p);
string ss="aaa,aaa,sss,d,d,ggggg,gg,e,eeee,ss,hhh,ff";
string[] cc = ss.Split(',');//由指定的字符进行分割
foreach(string itm in cc)
{
Console.WriteLine("{0}",itm);
}
string bb = "aaa,,,,vvv,v,v,v,v,,v,v";
string[] vv = bb.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);//用,分割,去掉空白
foreach(string iii in vv)
{
Console.WriteLine("{0}",iii);
}
*/
//练习27
/*
string[] text = System.IO.File.ReadAllLines(@"d:\text.txt", Encoding.Default);
string maxname = "";
int maxchengji = -1;
string age = "";
foreach(string i in text)
{
string[] line = i.Split('|');
string name=line[0];
age=line[1];
string chengji=line[2];
int score = Convert.ToInt32(chengji);
if(score>maxchengji)
{
maxname = name;
maxchengji = score;
}
Console.WriteLine("姓名{0}年龄{1}成绩{2}", name, age, chengji);
}
Console.WriteLine("最大分数的选手的姓名{0}年龄{1}成绩{2}",maxname,age,maxchengji);
*/
//练习28
/*
string s = "洋洋是个好女孩,洋洋最喜欢我,洋洋最喜欢吃西瓜。";
s = s.Replace("洋洋", "乔洋");//将前一个字符串用后一个进行替换
string name = "http://www.baidu.com";
string newname = name.Substring(7);//此函数作用是检索字符串,从指定位置开始构成新的字符串
Console.WriteLine(newname);
Console.WriteLine(s);
*/
//练习29
/*
string s = "社会真黑暗啊!!!!!!!!!!";
if(s.Contains("社会")||s.Contains("黑暗"))//contains函数表示字符串中包含什么!
{
Console.WriteLine("对不起,请文明用语!!!!!!");
}
string wangzhi = "http://www.cctvvvv.com";
if (wangzhi.StartsWith("http://") || wangzhi.StartsWith("https://"))//startwith函数表示字符串开头包含什么
{
Console.WriteLine("是网址");
}
else
{
Console.WriteLine("不是网址");
}
*/
//练习30
/*
string s = "萨克拉附近路口撒旦路口监控阿斯科罚金阿斯利康就";
int weizhi = s.IndexOf("路口");//返回字符串在字符串中出现的第一次的时候的位置,如果不存在返回-1
Console.WriteLine(weizhi);
Console.WriteLine("请输入,输入后将会倒过来");
string read= Console.ReadLine();
for (int i = read.Length - 1; i >= 0;i-- )
{
Console.Write(read[i]);
}
Console.WriteLine("请输入几句话有惊喜,记得用空格隔开奥");
string line = Console.ReadLine();
string[] good = line.Split(' ');//利用空格进行分割字符串,依次存入数组中
for (int y = good.Length - 1; y >= 0;y-- )//从数组最后元素依次输出到最开始元素
{
Console.Write(good[y]+" ");
}
Console.WriteLine("请输入你的邮箱:");
string emal = Console.ReadLine();
int mm = emal.IndexOf("@");//字符串中第一个@出现的位置
string name = emal.Substring(0,mm);//截取从开始到第一个@出现之前的字符串
Console.WriteLine(name);
string yuming = emal.Substring(mm +1);//截取@之后的字符串
Console.WriteLine(yuming);
*/
//练习31
/*
string s=getmomo(@"d:/text.txt","娃娃");
Console.WriteLine(s);
*/
//练习32 新机器人
/*
Console.WriteLine("大家好我是机器人");
while (true)
{
string str = Console.ReadLine();
if (str.StartsWith("天气"))
{
string city = str.Substring(2);
Console.WriteLine("{0}的天气很好",city);
}
else if(str.StartsWith("88")||(str.Contains("再见")||str.Contains("拜拜")))
{
Console.WriteLine("byebye");
return;
}
else if (str.Contains("你") || (str.Contains("名字") || str.Contains("姓名")))
{
Console.WriteLine("请不要迷恋哥");
}
else
{
Console.WriteLine("对不起,你输入的是火星文,请重新输入");
str = Console.ReadLine();
}
}
*/
//练习33
Console.ReadKey();
}
//练习21中调用的函数
/*
static int Sum(int[] shuzi)
{
int he = 0;
foreach(int i in shuzi)
{
he = he + i;
}
return he;//返回值必须和函数类型一致。
}
*/
//练习22调用的函数
/*
static string Join(string [] names,string zifu)
{
string shuchu = "";
for (int i = 0; i < names.Length-1;i++ )
{
shuchu = shuchu + names[i] + zifu;
}
if(names.Length>0)
{
shuchu=shuchu+names[names.Length-1];
}
return shuchu;
}
*/
//练习23调用的函数
/*
static void SayHello(string name,params string [] xiaoming)//可变参数与固定参数合用
{
Console.WriteLine(name);
foreach(string i in xiaoming)
{
Console.WriteLine("{0}",i);
}
}
*/
/*
static void Love(string name)
{
Console.WriteLine("我爱{0}",name);
}
static void Love()
{
Console.WriteLine("我谁都不爱");
}
static string Love(string name,string no)
{
return name + "是" + no + "岁";
}
*/
//练习31需要的函数
/*
static string getmomo(string filename,string panduanname)
{
string [] str= System.IO.File.ReadAllLines(filename,Encoding.Default);
foreach(string line in str)
{
string[] pp = line.Split('=');
string name=pp[0];
string value=pp[1];
if (name.Trim() == panduanname)
{
return value.Trim();
}
}
return "错误";
}
*/
}
}