C#考试题库
简单题
- 把输入的字符串中的内容逆置,并保存到新字符串,并输出新字符串的内容。
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串");
string tempStr = Console.ReadLine();
string lastStr = "";
for (int count = tempStr.Length - 1; count >= 0; count--)
{
lastStr += tempStr[count];
}
Console.WriteLine($"逆置后的字符串为{lastStr}");
}
-
已知三角形三条边长a,b,c,三边由用户输入,编程判断a、b、c的值是否构成三角形,如构成三角形,则计算并输出三角形的面积,否则
输出“不能构成三角形”计算三角形面积公式为:s=0.5*(a+b+c)
area=
求三角形的面积area。
static void Main(string[] args)
{
Console.WriteLine("请输入三角形的三条边长a、b、c (用空格分开):");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++)
{
tempInt[count] = int.Parse(tempStr[count]);
}
int a = tempInt[0];
int b = tempInt[1];
int c = tempInt[2];
if (a + b < c || b + c < a || a + c < b || a - b > c || b - c > a || a - c > b )
{
Console.WriteLine("三边不能构成三角形");
return;
}
double area = 0.5 * (a + b + c);
Console.WriteLine($"三角形的面积为{area}");
}
- 输入一个字符串str1,删除str1中其中所有的0-9的数字字符,输出处理后的字符串。
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串,删除字符串中的数字");
string tempStr = Console.ReadLine();
string result = "";
foreach (char item in tempStr) {
if (!(item > 48 && item < 57)) {
result += item;
}
}
Console.WriteLine(result);
}
- 输入10个数,计算平均值,统计低于平均值数据个数并把低于平均值的数据输出。
static void Main(string[] args)
{
Console.WriteLine("请输入十个数字,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] temInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++) {
temInt[count] = int.Parse(tempStr[count]);
}
int sum = 0;
foreach (int item in temInt) {
sum += item;
}
int average = sum / temInt.Length;
Console.WriteLine($"平均数 {average}");
ArrayList minList = new ArrayList();
foreach (int item in temInt)
{
if (item < average) {
minList.Add(item);
}
}
Console.WriteLine($"小于平均数的个数为 {minList.Count}");
Console.WriteLine("小于平均数的为");
foreach (int item in minList)
{
Console.WriteLine(item);
}
}
- 输入10个数,计算平均值,统计高于平均值数据个数并把高于平均值的数据输出。
static void Main(string[] args)
{
Console.WriteLine("请输入十个数字,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] temInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++)
{
temInt[count] = int.Parse(tempStr[count]);
}
int sum = 0;
foreach (int item in temInt)
{
sum += item;
}
int average = sum / temInt.Length;
Console.WriteLine($"平均数 {average}");
ArrayList maxList = new ArrayList();
foreach (int item in temInt)
{
if (item > average)
{
maxList.Add(item);
}
}
Console.WriteLine($"大于平均数的个数为 {maxList.Count}");
Console.WriteLine("大于平均数的为");
foreach (int item in maxList)
{
Console.WriteLine(item);
}
}
- 输入一些整数,找出其中最大数和次最大数。
static void Main(string[] args)
{
Console.WriteLine("请输入一些数字,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++)
{
tempInt[count] = int.Parse(tempStr[count]);
}
for (int i = 0; i < tempInt.Length - 1; i++) {
for (int j = i; j < tempInt.Length - 1; j++) {
if (tempInt[j] > tempInt[j + 1]) {
int temp = tempInt[j + 1];
tempInt[j + 1] = tempInt[j];
tempInt[j] = temp;
}
}
}
Console.WriteLine($"最大值 {tempInt[tempInt.Length - 1]},次最大值 {tempInt[tempInt.Length - 2]}");
}
- 输入一些整数,找出其中最小数和次最小数。
static void Main(string[] args)
{
Console.WriteLine("请输入一些数字,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++)
{
tempInt[count] = int.Parse(tempStr[count]);
}
for (int i = 0; i < tempInt.Length - 1; i++) {
for (int j = i; j < tempInt.Length - 1; j++) {
if (tempInt[j] > tempInt[j + 1]) {
int temp = tempInt[j + 1];
tempInt[j + 1] = tempInt[j];
tempInt[j] = temp;
}
}
}
Console.WriteLine($"最小值 {tempInt[0]},次最小值 {tempInt[1]}");
}
- 输入若干有序的正整数,对于相同的数据只保留一个,输出保留的数据。例如,输入数据是: 2,2,2,3,3,4,5,5,6,6,8,8,9,9,9,10,10,10
最终的输出结果是: 2,3,4,5,6,8,9,10。
static void Main(string[] args)
{
Console.WriteLine("请输入一些数字,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int count = 0; count < tempStr.Length; count++)
{
tempInt[count] = int.Parse(tempStr[count]);
}
ArrayList tempList = new ArrayList();
foreach (int item in tempInt) {
if (!tempList.Contains(item)) {
tempList.Add(item);
}
}
Console.WriteLine("不重复的数据为:");
foreach (int item in tempList) {
Console.WriteLine(item);
}
}
- 输入一个字符串,判断如果全是数字,将其转换成为一个整数,若包含其他符号,给出错误提示。
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");
string tempStr = Console.ReadLine();
foreach (char item in tempStr) {
if (item < 48 || item > 57) {
Console.WriteLine("error 不是存数字");
return;
}
}
int tempInt = int.Parse(tempStr);
Console.WriteLine($"转化为数字 {tempInt}");
}
- 输入20个正整数,分别统计并输出其中的奇数和偶数的个数,并分类输出所有奇数和偶数。
static void Main(string[] args)
{
Console.WriteLine("请输入20个正整数,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int i = 0; i < tempInt.Length; i++)
{
tempInt[i] = int.Parse(tempStr[i]);
}
ArrayList firstList = new ArrayList();
ArrayList secondList = new ArrayList();
foreach (int item in tempInt)
{
if (item % 2 == 0)
{
firstList.Add(item);
}
else {
secondList.Add(item);
}
}
Console.WriteLine($"偶数的个数为 {firstList.Count},奇数的个数为 {secondList.Count}");
Console.WriteLine("偶数为:");
foreach (int item in firstList)
{
Console.WriteLine(item);
}
Console.WriteLine("奇数为:");
foreach (int item in secondList) {
Console.WriteLine(item);
}
}
- 从终端输入5个数,按从小到大的顺序输出。
static void Main(string[] args)
{
Console.WriteLine("请输入5个数,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int i = 0; i < tempInt.Length; i++)
{
tempInt[i] = int.Parse(tempStr[i]);
}
for (int i = 0; i < tempInt.Length - 1; i++) {
for (int j = 0; j < tempInt.Length - 1; j++) {
if (tempInt[j] > tempInt[j + 1]) {
int temp = tempInt[j];
tempInt[j] = tempInt[j + 1];
tempInt[j + 1] = temp;
}
}
}
foreach (int item in tempInt) {
Console.WriteLine(item);
}
}
- 从键盘读入20个数据到数组中,统计其中负数的个数,并计算这些负数之和。
static void Main(string[] args)
{
Console.WriteLine("请输入20个数,用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int i = 0; i < tempInt.Length; i++)
{
tempInt[i] = int.Parse(tempStr[i]);
}
ArrayList list = new ArrayList();
foreach (int item in tempInt)
{
if (item < 0) {
list.Add(item);
}
}
int sum = 0;
foreach (int item in list)
{
sum += item;
}
Console.WriteLine($"负数个数为 {list.Count},负数之和为 {sum}");
}
- 求n以内(不包括n)不能同时被2和5整除(能被2或者5整除但不能同时被整除)的所有自然数之和的平方根s,n从键盘输入。
static void Main(string[] args)
{
Console.WriteLine("请输入一个n,求n以内,能被2或者5整除但不能同时被整除的数");
int n = int.Parse(Console.ReadLine());
ArrayList list = new ArrayList();
for (int i = 0; i < n; i++)
{
if ((i % 2 == 0 || i % 5 == 0) && !(i % 2 == 0 && i % 5 == 0)) {
list.Add(i);
}
}
int sum = 0;
foreach (int item in list)
{
sum += item;
}
int s = sum * sum;
Console.WriteLine($"所有自然数之和的平方根 {s}");
}
- 输入1~7之间的一个数字,输出它对应的星期几。例如输入1 输出Monday。
static void Main(string[] args)
{
Console.WriteLine("请输入1-7之间的一个数字");
int tempInt = int.Parse(Console.ReadLine());
switch (tempInt)
{
case 1:
Console.WriteLine("星期一");
break;
case 2:
Console.WriteLine("星期二");
break;
case 3:
Console.WriteLine("星期三");
break;
case 4:
Console.WriteLine("星期四");
break;
case 5:
Console.WriteLine("星期五");
break;
case 6:
Console.WriteLine("星期六");
break;
case 7:
Console.WriteLine("星期七");
break;
}
}
- 个位数为8且能被4整除但不能被7整除的二位自然数共有多少个,统计个数,并输出这些数。
static void Main(string[] args)
{
for (int i = 10; i < 100; i++)
{
if (i % 4 == 0 && i % 7 != 0 && i % 10 == 8)
{
Console.WriteLine(i);
}
}
}
- 输入一个字符串,用foreach语句计算输入的字符串的长度,并显示长度。
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串计算长度:");
string tempStr = Console.ReadLine();
int length = 0;
foreach (char item in tempStr) {
length++;
}
Console.WriteLine($"字符串的长度为: {length}");
}
- 输入7个数,分别统计其中正数、负数、零的个数。
static void Main(string[] args)
{
Console.WriteLine("请输入7个数字,并用空格隔开:");
string[] tempStr = Console.ReadLine().Split(" ");
int[] tempInt = new int[tempStr.Length];
for (int i = 0; i < tempInt.Length; i++)
{
tempInt[i] = int.Parse(tempStr[i]);
}
ArrayList firstList = new ArrayList();
ArrayList secondList = new ArrayList();
ArrayList thirdList = new ArrayList();
foreach (int item in tempInt) {
if (item > 0) {
firstList.Add(item);
}else if (item < 0){
secondList.Add(item);
}else {
thirdList.Add(item);
}
}
Console.WriteLine($"正数个数 {firstList.Count},负数个数 {secondList.Count},零个数 {thirdList.Count}");
}
方法题
- 统计一段英文中每个字母和该字母个数
private static void Main(string[] args) {
TestDemo testDemo = new TestDemo();
Console.WriteLine("请输入一段英文短文");
string sentence = Console.ReadLine();
Hashtable collection = testDemo.countWord(sentence);
foreach (char word in collection.Keys) {
Console.WriteLine($"{word} {collection[word]}");
}
}
private Hashtable countWord(string sentence) {
Hashtable collection = new Hashtable();
foreach(char word in sentence)
{
if ((word > 65 && word < 90) || (word > 97 && word < 122)) {
if (collection[word] == null) collection[word] = 1;
else collection[word] = (int) collection[word] + 1;
}
}
return collection;
}
数据库代码
- 查询
public static DataSet querySql()
{
SqlConnection conn = new SqlConnection("server=.;database=AccountDB;uid=sa;pwd=123456");
string sql = "select * from tb_user";
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet dataSet = new DataSet();
try
{
conn.Open();
adapter.Fill(dataSet);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
return dataSet;
}
- 更新
public static int commitSql(User user) {
SqlConnection conn = new SqlConnection("server=.;database=AccountDB;uid=sa;pwd=123456");
string sql = String.Format($"insert into tb_user(username, password, state) values({user.UserName}, {user.Password}, {user.State})");
SqlCommand cmd = new SqlCommand(sql, conn);
int result = 0;
try {
conn.Open();
result = cmd.ExecuteNonQuery();
}catch( Exception ex )
{
Console.WriteLine( ex.ToString() );
}finally {
conn.Close();
}
return result;
}