您的位置:首页 > 编程语言 > C#

C#练习

2017-01-10 20:13 246 查看

[b]C#简单程序练习[/b]


说明:学习之余温习几道经典基础知识题,将其记录下来,以供初学者参考。

1,题目:求出0-1000中能被7整除的数,并计算输出每五个数的和:

int Count = 0;
int Sum = 0;
for (int i = 1; i <= 1000; i++)
{
if (i % 7 == 0)
{
Sum += i;
Count++;
Console.Write("{0} ", i);
}
if (Count == 5)
{
Console.Write("和为{0} ", Sum);
Sum = 0;
Count = 0;
Console.WriteLine();
}

}
Console.Read();


运行截图:



题目2:编写一个类,其中包含一个排序的方法 Sort(), 当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

代码:

Console.Write("输入字符串");
string Str = Console.ReadLine();
Paixu Pa = new Paixu();
Pa.Sort(Str);

//此类单独建在cs文件中
class Paixu
{
//数字的从大到小的排序

public void Sort(string str)
{

char[] num = str.ToCharArray();
bool t = true;
for(int i=0;i<num.Length;i++)
{
if(num[i]<'0'||num[i]>'9')
{
t = false;
break;
}

}
if (t == true)
{
for (int i = 0; i < num.Length; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
char tem = num[j];
num[j] = num[j + 1];
num[j + 1] = tem;
}
}
}
string Num = new string(num);
Console.WriteLine(Num);
}
else
{
for(int i = 0; i < num.Length / 2; i++)
{
char tem = num[i];
num[i] = num[num.Length - 1 - i];
num[num.Length - 1 - i] = tem;
}
string str2 = new string(num);
Console.WriteLine(str2);
}
}


运行截图:





题目3:

编写一个矩形类,私有数据成员为举行的长(len)和宽(wid),无参构造函数将len和wid设置为0,有参构造函数设置和的值,另外,
类还包括矩形的周长、求面积、取举行的长度、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。


class Rectangle
{
//无参定义len 和wid为0
double length = 0;
double width = 0;
//有参构造函数
public double Length
{
get { return length; }
set { length = value; }
}
public double Width
{
get { return width; }
set { width = value; }
}

//构造函数周长
public double Perimeter
{
get
{
return ((length + width) * 2);
}
}
//构造函数面积
public double Area
{
get
{
return (length * width);
}
}
//方法,取矩形的长度和宽度
public void LenAndWid(double length, double width)
{
this.length = length;
this.width = width;
}
//修改长度对应的形参值
public void ELength(double Len)
{
this.length = Len;
}
//修改宽度对应的形参值
public void EWidth(double Wid)
{
this.width = Wid;
}
//取矩形的长
public double GetLength()
{
return this.length;
}
//取矩形的宽
public double GetWidth()
{
return this.width;
}
}


截图:



题目4:

编写一个控制台应用程序,接收一个长度大于3的字符串,完成 下列功能:

1) 输出字符串长度。 2) 输出字符串中第一个出现字母a的位置。 3) 在字符串的第3个字符后面插入子串“hello”,输出新字符串。

4) 将字符串“hello”替换成“me”,输出新字符串。 5) 以字符“m”为分隔符,将字符串分离,并输出分离后的字符 串。
代码:(cs文件)

class Class4
{
/*说明:接受长度大于3的字符串
1:输出字符串的长度
2:输出字符串中出现第一个字母a的位置
3:在字符串的第三个字符后加入hello,输出新的的字符串
4:将字符串hello替换成me,输出新的字符串
5:以m为分隔符将字符串分离,并输出分离后的字符串
*/
//构造函数获取输入的字符串
string str = string.Empty;
public string Str
{
get { return str; }
set { str = value; }
}
//构造方法输出字符串的长度
public string Length()
{
char[] ss = this.str.ToCharArray();//将输入的字符串转换成数组
int len = ss.Length;
return len.ToString();

}
//输出字符串中第一个字母a的位置
public string aLoction()
{
char[] ss = this.str.ToCharArray();
int loc =1;
for (int i =0; i < ss.Length; i++)
{
if (ss[i] == 'a')
loc = i+1;
// break;
}
return loc.ToString();
}
//在字符串的第三个字母后加入字符串hello,并输出
public string addStr()
{
string addstr = str.Insert(3, "hello");
return addstr;

}
//将hello替换成me,输出新的字符串
public string ediStr(string newstr)
{
string old = "hello";
string ne = "me";
string edistr = newstr.Replace(old,ne);
return edistr;
}
//用字母m将字符串分割,输出分割后的字符串
public string splitStr(string ss)
{
string[] newstr = ss.Split('m');
string aa = string.Empty;
for(int i = 0; i < newstr.Length; i++)
{
// Console.Write(newstr[i]);
aa = newstr[i];
}
return aa;

}


main函数:

Console.Write("输入字符串");
string Mystr = Console.ReadLine();
Class4 str = new Class4();//实例化处理字符串的类
str.Str = Mystr;
Console.Write("1:字符串的长度为{0}\n2:第一次出现A或a 的位置是;{1}\n3:插入hello后的字符串{2}\n4:将hello替换成me后的字符串{3}\n"
, str.Length(), str.aLoction(), str.addStr(), str.ediStr(str.addStr()));
Console.Write("5:用m分割后的字符串{0}",str.splitStr(str.ediStr(str.addStr())));


运行截图



不足之处还望指点!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: