您的位置:首页 > 其它

常量,转换,字符串,逻辑语句,循环,数组

2014-07-26 23:56 183 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace T2
{
class Program
{
//常量(必须在声明时赋值,赋值后不能修改)
//static void Main(string[] args)
//{
// const int num = 20;
// //num = 30;//出错
// Console.WriteLine(num);
//}

//类型转换
//static void Main(string[] args)
//{
// //隐式与显示转换
// //double d = 5;//隐式转换(自动转换)
// //int num = (int)21.6;//显示转换(强制转换,容易造成数据丢失,不能四舍五入)
// //Console.WriteLine(d);
// //Console.WriteLine(num);

// //Parse方法(字符串转换为其他数据类型)
// //string intStr = "23";
// //int num = int.Parse(intStr);
// //string douStr = "3.1415";
// //double d = double.Parse(douStr);
// //Console.WriteLine(num);
// //Console.WriteLine(d);

// //ToString方法(将其他类型转换为字符串)
// //double d = 25.3;
// //string result = d.ToString();
// //Console.WriteLine(result+10);

// /*Convert类进行数据类型转换:
// * 特点:将任意类型转换为其他类型
// * 注意:1、ToInt16、ToInt32、ToInt64代表要转换为short、int、long
// * 2、ToSingle代表float
// * 3、能进行四舍五入
// */
// //int num1 = Convert.ToInt32("6");
// //double d = Convert.ToDouble("21.53");
// //int num2 = Convert.ToInt32(35.566);
// //string str1 = Convert.ToString(10);
// //string str2 = Convert.ToString(true);
// //bool b = Convert.ToBoolean("true");
// //float f = Convert.ToSingle("21.2");
// //Console.WriteLine(num2);
//}

//嵌套if
//static void Main(string[] args)
//{
// double price = 4000.00;
// int month;
// int grade;
// Console.WriteLine("请输入月份:");
// month = Convert.ToInt32(Console.ReadLine());
// Console.WriteLine("请输入您要购买的类型?(1、经济舱 2、头等舱)");
// grade = Convert.ToInt32(Console.ReadLine());
// if (month >= 5 && month <= 10)
// {
// if (grade == 1)
// {
// Console.WriteLine("价格为:" + price * 0.75);
// }
// else
// {
// Console.WriteLine("价格为:" + price * 0.9);
// }
// }
// else
// {
// if (grade == 1)
// {
// Console.WriteLine("价格为:" + price * 0.3);
// }
// else
// {
// Console.WriteLine("价格为:" + price * 0.6);
// }
// }
//}

/*C#与C中switch的区别:
* 1、C中只能匹配整型、字符型,C#中除了整型、字符型还可以是字符串
* 2、C#中case里面如果有一句代码就必须要加break
* 3、C#中case里面如果没有代码,可以将break省略,实现穿透效果
* 4、C#中default放在最后,如果里面没代码,也必须加break
*/
//static void Main(string[] args)
//{
// string week = "星期日";
// switch(week){

// case "星期一":
// case "星期二":
// case "星期三":
// case "星期四":
// case "星期五":
// Console.WriteLine("工作日!");
// break;
// case "星期六":
// Console.WriteLine("休息日!");
// break;
// case "星期日":
// Console.WriteLine("休息日!");
// break;
// default:
// Console.WriteLine("输入有误!");
// break;
// }
//}

//foreach循环的使用
//static void Main(string[] args)
//{
// Console.WriteLine("请输入一个字符串:");
// string str = Console.ReadLine();
// //遍历字符串中的字符(方法一)
// //for (int i = 0; i < str.Length; i++)
// //{
// // Console.WriteLine(str[i]);
// //}
// //遍历字符串中的字符(方法二)
// foreach(char c in str)
// {
// Console.WriteLine(c);
// }
//}

//多重循环(打印直角三角形)
//static void Main(string[] args)
//{
// for (int i = 1; i <= 5;i++ )
// {
// for (int j = 1; j <= i;j++ )
// {
// Console.Write("*");
// }
// Console.WriteLine();//换行
// }
//}

//多重循环(每班的平均分)
//static void Main(string[] args)
//{
// int total = 0;//记录每班的总分
// for (int i = 1; i <= 3;i++ )
// {
// Console.WriteLine("请输入第{0}班的成绩:",i);
// for (int j = 1; j <= 4;j++ )
// {
// Console.WriteLine("请输入第{0}个学生的成绩:",j);
// total += Convert.ToInt32(Console.ReadLine());
// }
// Console.WriteLine("第{0}班的平均成绩为:{1}",i,total/4);
// total = 0;//总分清空,方便下个班级平均分的运算
// }
//}

/*一维数组
* 注意:声明时要将数据类型和方括号放在一起 例如:double[] scores;
*/
//static void Main(string[] args)
//{
// //先创建数组,再初始化
// //int[] num = new int[3];//创建一个长度为3的整型数组
// //num[0] = 10;
// //num[1] = 12;
// //num[2] = 30;
// //num[3] = 52;//下标越界

// //创建数组同时进行初始化
// //int[] num1 = new int[3] {10,12,30};//长度可以加也可以不加,不加时会通过花括号的元素来确定长度
// //int[] num2 = { 10, 12, 30 };
// //Console.WriteLine(num2[3]);

// //遍历一维数组
// //int[] arr = {3,5,7,10};
// //for (int i = 0; i < arr.Length;i++)
// //{
// // Console.WriteLine(arr[i]);
// //}
// //foreach (int num in arr)
// //{
// // Console.WriteLine(num);
// //}
//}

//字符串的使用
//static void Main(string[] args)
//{
// //Format方法的使用,下面2句代码输出的结果和Console.WriteLine("姓名:{0},年龄:{1}", "张三", 23);的一样
// //string result = string.Format("姓名:{0},年龄:{1}", "张三", 23);
// //Console.WriteLine(result);

// //Substring截取字符串
// //string str = "abcdef";
// //string result1 = str.Substring(2);//从下标2一直截取到最后
// //Console.WriteLine(result1);
// //string result2 = str.Substring(2, 3);//参数:开始截取的下标 参数2:要截取多少个字符
// //Console.WriteLine(result2);

// //Split根据指定字符将字符串分割成字符串数组
// //string str = "oh my god";
// //string[] strs = str.Split(' ');//使用空格字符将str进行分割
// //foreach (string item in strs)
// //{
// // Console.WriteLine(item);
// //}

// //Join将字符串数组中的元素用指定分隔符进行连接
// //string[] s = { "oh", "my", "god" };
// //string result = string.Join("_",s);//Join是静态方法所以直接用string调用
// //Console.WriteLine(result);

// //Replace将字符串中指定的字符或字符串用新的字符或字符串代替
// //string str = "aabbcc";
// //string result = str.Replace("bb", "ee");//参数1:旧的字符串或字符 参数2:新的字符串或字符
// //Console.WriteLine(result);

// /*IndexOf查找指定字符或字符串第一次出现的索引位置
// *LastIndexOf查找指定字符或字符串最后一次出现的索引位置
// *注意:上面2个方法当找不到指定的字符或字符串时,会返回-1
// */
// //string str = "hello sve";
// //int index = str.IndexOf('l');
// //Console.WriteLine(index);
// //int lastIndex = str.LastIndexOf('e');
// //Console.WriteLine(lastIndex);

// //ToLower将字符串转小写,ToUpper将字符串转大写
// //string str = "Hello World";
// //string lowerStr = str.ToLower();
// //Console.WriteLine(lowerStr);
// //string upperStr = str.ToUpper();
// //Console.WriteLine(upperStr);
//}

//获取邮箱用户名
static void Main(string[] args)
{
string email;
string isContinue;//是否继续
do
{
Console.WriteLine("请输入您的邮箱:");
email = Console.ReadLine();
int index = email.IndexOf('@');
string result = email.Substring(0,index);
Console.WriteLine("您的用户名是:"+result);
Console.WriteLine("是否继续?");
isContinue = Console.ReadLine();
}while(isContinue.ToUpper()=="YES");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: