Console.Write(\请输入一个字符串(直接回车退出):\);
//读入键盘输入的字符,若为空格则跳出死循环,否则显示输入数据继续循环 string s = Console.ReadLine(); if (s.Length == 0) break;
Console.WriteLine(\你输入的是: {0}\, s); } } } }
(8)循环跳转综合语句实例
namespace CycleAndJumpingExample {
class Program {
static void Main() {
while (true) {
Console.Write(\请输入一个字符串(q结束):\); //从键盘接收一行信息
string s = Console.ReadLine(); if (s.Length == 0) continue;
//如果接收的首字符等于Q或者q,则退出循环
if (s.Substring(0, 1).ToUpper() == \) break; int letterIndex = -1, digitIndex = -1; bool checkLetter, checkDigit; checkLetter = checkDigit = true;
// s.Length是字符串长度,即字符串中字符的个数 //例如:“ab章三c”的长度为5
//注意字符串由Unicode字符组成,即每一个汉字和字母均占两字节 for (int i = 0; i < s.Length; i++) {
//如果已经找到首次出现的字母位置和首次出现的数字位置后,则 //强制退出for循环
if (!checkLetter && !checkDigit) break; if (checkLetter)
//如果第i个字符是首次出现的字母,则记住字母位置 if (Char.IsLetter(s[i])) {
letterIndex = i; checkLetter = false; }
if (checkDigit == true) {
//如果第i个字符是首次出现的数字,则记住数字位置 if (Char.IsDigit(s[i])) {
digitIndex = i;
checkDigit = false; } } }
if (letterIndex > -1) {
Console.WriteLine(\包含的第一个字母是'{0}'。\, s[letterIndex]); } else {
Console.WriteLine(\字符串中不包含字母。\); }
if (digitIndex > -1) {
Console.WriteLine(\包含的第一个数字是'{0}'。\, s[digitIndex]); } else {
Console.WriteLine(\字符串中不包含数字。\); } }
Console.ReadLine(); } } }
break语句的功能是退出最近的switch、while、do-while、for或foreach语句
continue语句的功能是不再执行continue语句后面循环快内剩余语句,而是将控制直接传递给下一次循环,此语句可以用在while、do-while、for或foreach语句块内部。 10、try catch finally用法
如果try后面有finally块,不论是否出现异常,也不论是否有catch块,finally块总会执行的,即使在try内使用跳转语句,或return语句,也不能避免finally块的执行,一般在finally块中作释放资源的操作,如关闭打开的文件,关闭与数据库的连接等。 try-catch –finally常用形式 try {
语句序列 }
catch(异常类型,标识符) {
异常处理 }
finally {
语句序列 }
11、课后习题
2、C#语言中不同整型之间进行转换的原则是什么?
答:在整型之间进行转换时,小范围类型可以隐式转换为大范围类型,但大范围类型转换为小范围类
型时需要使用显式转换。
3、简述装箱和拆箱的过程。
答:装箱是将值类型隐式转换为object类型或者转换为由该值类型实现了的接口类型。装箱一个数值
会为其分配一个对象实例,并把该数值拷贝到新对象中。拆箱是显式的把object类型转换为值类型,或者把值类型实现了的接口类转换为该值类型。 4、分别写出下列语句执行的结果
Console.WriteLine(“{0}--{0:P}good”,12.34F); 运行结果:12.34--1,234.00%good
Console.WriteLine(“{0}--{0:####}good”,0);
运行结果:0--good
Console.WriteLine(“{0}--{0:00000}good”,456); 三、常用数据类型的用法
1、字符串的常用用法:求字符串的长度、Insert、Contains、IndexOf、Join、Split
using System; class Test4 {
public static void Main() {
string str = \
while (str.Length <= 3) {
Console.Write(\请输入一个长度大于3的字符串:\ str = Console.ReadLine(); }
//(1)
Console.WriteLine(\字符串的长度为:{0}\ //(2)
int i = str.IndexOf('a'); if (i > -1) {
Console.WriteLine(\第一个出现字母a的位置是:{0}\ } else {
Console.WriteLine(\字符串中不包含字母a。\ }
//(3)
string str1 = str.Insert(3, \ //在第3个(初始序号为)字符前插入hello Console.WriteLine(\插入hello后的结果为:{0}\ //(4)
string str2 = str1.Replace(\
Console.WriteLine(\将hello替换为me后的结果为:{0}\ //(5)
string[] arr = str2.Split('m');
Console.WriteLine(\以m为分隔符分离后的字符串有:\ for (int j = 0; j < arr.Length; j++) {
Console.WriteLine(arr[j]); } } }
2、一维数组定义及初始化方法
Int[] a=new int [30];//创建一个包含30个int型元素的数组a,并在定义时指定元素个数为30 strin[] b;
int number=int.Parse(Console.ReadLine());//从键盘接收元素个数;
b=new String[number];//指定数组b共有number个元素,也可以在声明语句中直接用简化的形式为各元
素赋初值
string[] mystring=new string[]{“frist”,”second”,”thrid”}; 或string[] mystring={“frist”,”second”,”thrid”};
注意:不带new运算符的简化形式只能用在声明语句中 string[] mystring;
mystring={“frist”,”second”,”thrid”};//错误
mystring=new string[]{“frist”,”second”,”thrid”};//正确
3、枚举定义中注意enum Number:byte{x1=255,x2}这种形式是错误的
因为在枚举定义常量中,后面常量值系统会自动加一,即x1=255,x2=256,而byte类型的取值范围只
能是0~255
4、List
namespace ListExample {
class Program {
static void Main() {
List
list.Insert(0, \王五\);
if (list.Contains(\赵六\) == false) {
list.Add(\赵六\); }
foreach (var item in list) {
Console.WriteLine(item); }
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(\, i, list[i]); }
list.Remove(\张三\); list.Clear();
Console.ReadLine(); } } }
5、课后习题 4、编写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能。 (1)输出字符串的长度
(2)输出字符串中第一个出现字母a的位置
(3)在字符串的第三个字符后面插入字符串“hello”,输出新字符串 (4)将字符串“hello”替换为“me”输出新字符串
(5)以字符串“m”为分隔符,将字符串分离,并输出分离后的字符串
using System; class Test4 {
public static void Main() {
string str = \
while (str.Length <= 3) {
Console.Write(\请输入一个长度大于3的字符串:\ str = Console.ReadLine(); }
//(1)
Console.WriteLine(\字符串的长度为:{0}\ //(2)
int i = str.IndexOf('a'); if (i > -1) {
Console.WriteLine(\第一个出现字母a的位置是:{0}\ } else {
Console.WriteLine(\字符串中不包含字母a。\ }
//(3)
string str1 = str.Insert(3, \ //在第3个(初始序号为)字符前插入hello Console.WriteLine(\插入hello后的结果为:{0}\ //(4)
string str2 = str1.Replace(\
Console.WriteLine(\将hello替换为me后的结果为:{0}\ //(5)
string[] arr = str2.Split('m');
Console.WriteLine(\以m为分隔符分离后的字符串有:\ for (int j = 0; j < arr.Length; j++) {
Console.WriteLine(arr[j]); } } }
四、面向对象编辑基础 1、类的成员有哪些?
有静态成员和实例成员
如何声明一个类,如何创建一个类实例。例4-1
namespace ClassExample {
public class Child {
// 字段
private int age; private string name; // 不带参数的构造函数 public Child() {
name = \; }
// 带参数的构造函数
public Child(string name, int age) {
this.name = name; this.age = age; }