摸底练习

练习题题目

  • 编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。判断依据如下:等级={优 (90~100分);良 (80~89分);中 (60~79分);差 (0~59分);}

代码1:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入您的分数");
            int score = ReadInt();
            if(score>=90){
                Console.WriteLine("您的成绩为优");
            }
            else if (score < 60) {
                Console.WriteLine("您的成绩为差");
            }
            else if (score > 70 && score < 80) {
                Console.WriteLine("您的成绩为良好");
            }
            Console.ReadKey();
           
        }
        static int ReadInt() {
            do
            {
                try {
                    int num = Convert.ToInt32(Console.ReadLine());
                    return num;
                }
                catch {
                    Console.WriteLine("输入有误,请重新输入");
                }
            } while (true);
        }
    }
View Code

代码2:

 class Program
    {
        static void Main(string[] args)
        {
            while(true){
                Console.Clear();
                Console.WriteLine("请问您多少分");
                int score = ReadInt();
                switch (score / 10)
                {
                    case 9: Console.Write("您的成绩为优"); break;
                    case 8: Console.Write("您的成绩为良好"); break;
                    case 7:
                    case 6:
                        Console.Write("您的成绩为一般"); break;
                    default:
                        Console.Write("您的成绩不及格"); break;
                }
                Console.Write("
按任意键继续");
                    Console.ReadKey();
            }
        }
        static int ReadInt()
        {
            do 
            {
                try
                {
                    int num = Convert.ToInt32(Console.ReadLine());
                    return num;
                }
                catch 
                {
                    Console.WriteLine("输入错误,重新输入吧");
                }
            } while (true);
        }
    }
View Code

 小结:代码2采用swith-case控制流结构要比if-else if-else结构更高效。

  • 编程输出九九乘法表。

 代码:打印九行九列,每一行的每一列依次为行数*列数,并且列数小于或等于要比其所在的行数。

 class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10;i++ )
            {
                Console.WriteLine();
                for (int j = 0; j < i; j++)
                {
                    Console.Write("{0}*{1}={2}	", i, j, i * j);
                }
            }
            Console.ReadKey();
        }
    }
View Code
  • 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回

代码1:根据字符的用ascil码判断首字母是否为大写字母,首字母在97-123之间的就是小写字母,将其减去32就得到首字母的小写。

   Console.WriteLine("请输入一个字符串");
            string str = Console.ReadLine();
            char ch = str[0];
            if (ch>97 &&ch<123)
            {
                ch =Convert.ToChar( ch - 32);
            }
            str = ch.ToString() + str.Substring(1);
            Console.WriteLine("您输入的字符串为{0}", str);
            Console.ReadKey(); 
View Code

代码2:更简单的办法就是取出首字母这个字符,将其转换为string类型,再调用string类的ToUpper方法将其转换成大写形式,最后将首字母与字符串的后面部分拼接起来

             Console.WriteLine("请输入一个字符串");
            string str = Console.ReadLine();
            string strF = str[0].ToString().ToUpper();
            str = strF + str.Substring(1);
            Console.WriteLine("您输入的字符串为{0}", str);
            Console.ReadKey(); 
View Code
  • 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换?

代码:

            int a = 10;
            int b = 20;

            //推荐用第一种方法
            int temp = a;
            a = b;
            b = temp;

            //a = a * b;
            //b = a / b;
            //a = a / b;

            //a = a ^ b;
            //b = a ^ b;
            //a = a ^ b;

           //a = b + (b = a) * 0;//c#和java里面都可以用
            Console.WriteLine("a为{0},b为{1}", a, b);
            Console.ReadKey();
View Code
  • 用方法来实现:将上题封装一个方法来做。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*)
 class Program
    {
        static void Main(string[] args)
        {
            int a = 10, b = 20;
            ChageValue(ref a, ref b);
            Console.WriteLine("a为{0},b为{1}", a, b);
            Console.ReadKey();
        }
        static void ChageValue(ref int num1,ref int num2)
        {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }
    }
View Code
  • 请用户输入一个字符串,计算字符串中的字符个数,并输出

代码1:原始做法,用算法。定义一个数组chs来存储子字符串的字符,定义一个数组nums来存储字符的个数。遍历字符串的每个字符,判断每次遍历的字符是否已经存在于chs数组中,如果存在则跳出循环,若是因为字符已经存在于chs数组的原因而跳出循环的话,此时循环的j(即循环的次数-1)肯定不等于字符串的长度。若等于则表示正常跳出循环,chs数组中不存在该字符,这时候就将该字符加入chs中(找到chs数组中第一个为的字符的序号,将这个序号存储我们遍历到的字符),并将nuns数组的对应的序号位置设定为1,以表示该字符出现了一次。若是遍历的字符已经存在chs数组中,则将该字符对应nuns数组上的个数加1。

  Console.WriteLine("请输入一个字符串");
            string s = Console.ReadLine();
            //考虑字符串出现不同的字符个数为s.Length个
            char[] chs = new char[s.Length];
            int[] nums = new int[s.Length];
            //遍历这个字符串
            for (int i = 0; i < s.Length; i++)
            {
                char obj = s[i];
                //判断chs中是否有字符obj,如果有存储这个字符的个数的nun数组中找出来,并将这个数加1
                //如果不存在,将这个字符加到chs数组中,并将这个字符位子相同的nums
                int j = -1;
                for (j = 0; j < s.Length; j++)
                {
                    if (obj == chs[j])
                    {
                        break;
                    }
                }
                if (j == s.Length)//表示该字符第一次出现,因为j=length说明是正常跳出循环
                {
                    for (int k = 0; k < chs.Length; k++)
                    {
                        if(chs[k]==''){
                        chs[k] = obj;
                        nums[k] = 1;
                        break;
                        }
                    }
                }
                else
                {
                    nums[j] += 1;
                }
            }
            for (int i = 0; i < chs.Length; i++)
            {
                if (chs[i] == '')
                {
                    break;
                }
                Console.WriteLine("字符{0}出现{1}次", chs[i], nums[i]);
            }
            Console.ReadKey(); 
View Code

代码2:原始做法的优化,定义了一个current变量来记录chs数组每次存储新字符时候的序号,及nums数组存储字符个数的序号位置。

   int current = -1;
            Console.WriteLine("请输入一个字符串");
            string str = Console.ReadLine();

            char[] chs = new char[str.Length];
            int[] nums = new int[str.Length];

            int i = -1;
            for (i = 0; i < str.Length; i++)
            { 
                char obj = str[i];
                int j = -1;
                for (j = 0; j < chs.Length; j++)
                {
                    if (obj == chs[j])
                    {
                        break;
                    }
                }
                if (j == str.Length)
                {
                    chs[++current] = obj;
                    nums[current] = 1;
                }
                else
                {
                    nums[j] += 1;
                }
            }
            for (int k = 0; k < current;k++ )
            {
                Console.WriteLine("字符{0}出现{1}次", chs[k], nums[k]);
            }
            Console.ReadKey();
View Code

代码3:调用封装好的类库Dictionary<string,int> dic来存储字符及出现的个数,若是该字符已经出现了,就找到dic<string str>将其存储的字符个数加一。若是没有出现过,就调用Dictionary类的Add()方法,将字符,个数(1)添加到dic字典里。

            Console.WriteLine("请输入字符串");
            string str = Console.ReadLine();
            Dictionary<char, int> dic = new Dictionary<char, int>();
            for (int i = 0; i < str.Length; i++)
            {
                if (dic.ContainsKey(str[i]))
                {
                    dic[str[i]]++;
                }
                else
                {
                    dic.Add(str[i], 1);
                }
            }
            foreach (var item in dic)
            {
                Console.Write("字符{0}出现的次数为{1}
", item.Key, item.Value);
            }
View Code
  • 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)。

 代码:知识点:Params 修饰数组参数 表示可变数组,既可以传一个数组,也可以穿传许多个数进去方法内部,方法会自动帮我们将所有数字整合成一个数组

  class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 2, 3, 4 ,5,6};
            Console.WriteLine("大数为{0}", FindMax(4, 9));
            Console.WriteLine("最大数为{0}", FindMax(array));
            Console.WriteLine("最大数为{0}",FindMax(1,2,3,4,4,6,8,9));
            Console.ReadKey();
        }
        static int FindMax(params int[] nums)//可变数组参数
        {
            int max = nums[0];
            for (int i = 1; i < nums.Length;i++ )
            {
                if (max < nums[i])
                {
                    max = nums[i];
                }
            }
            return max;
        }
        static int FindMax(int num1, int num2)
        {
          return  num1 > num2 ? num1 : num2;
        }
View Code
  • 用方法来实现:计算1-100之间的所有整数的和。用方法来实现:计算1-100之间的所有奇数的和。

代码:知识点:方法重载,枚举enum Tag{even=0,odd=1}枚举类型的实质就是uint

namespace 求和方法
{
    public enum Tag{
        Even=0,
        Odd=1
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("1-100的累加和为{0}", GetSum(0, 101));
            Console.WriteLine("1-100的奇数的累加和为{0}", GetSum(0, 101,Tag.Even));
            Console.ReadKey();
        }
        static int GetSum(int min,int max)
        {
            int sum = 0;
            for (int i = min; i < max;i++ )
            {
                sum += i;
            }

            return sum;
        }
        static int GetSum(int min,int max, Tag tag){
            int sum = 0;
            for (int i = min; i < max;i++ )
            {
                if (i%2==(int)tag)
                {
                    sum += i;
                }
            }
            return sum;
    }
View Code
  • 用方法来实现:判断一个给定的整数是否为“质数”。

代码:知识点:判断质数就遍历除以从2开始到本身-1数字,但其实通过数学分析,只要从2开始到该数字的开平方根遍历进行除法运算即可。

 static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数,我们将判断是否为质数");
            int num = ReadInt();
            bool yesno = Testzhishu(num);
            if (yesno)
            {
                Console.WriteLine("是质数");
            }
            else
            {
                Console.WriteLine("不是质数");
            }
            Console.ReadKey();
        }
        static bool Testzhishu(int num)
        {
            for (int i = 2; i <=Math.Sqrt(num) ;i++ )
            {
                if (num%i==0)
                {
                    return false;
                }
            }
            return true;
        }
        static int ReadInt()
        {
            do 
            {
                try
                {
                    int num = Convert.ToInt32(Console.ReadLine());
                    return num;
                }
                catch
                {
                    Console.WriteLine("输入有误,请重新输入");
                }
            } while (true);
        }
View Code
  • 用方法来实现:计算1-100之间的所有质数(素数)的和。

代码:定义一个sum变量,存储累加和,遍历1-100,对每个数进行质数判断,若是质数,将其加到sum上。

 static void Main(string[] args)
        {
            int count = 0;
            int sum = 0;
            for (int i = 1; i < 101;i++ )
            {
               if (Testzhishu(i))
               {
                   Console.Write(i + "	");
                   sum += i;
                   count++;
                   if (count%5==0)
                   {
                       Console.WriteLine();
                   }
               }
            }
            Console.WriteLine("所有的质数的总和为{0}", sum);
            Console.ReadKey();
        }
        static bool Testzhishu(int num)
        {
            for (int i = 2; i < Math.Sqrt(num);i++ )
            {
                if (num%i==0)
                {
                    return false;
                }
            }
            return true;
        }
View Code
  • 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。

 代码:首先设置字符串数组的第一个字符串元素为最长的字符max,再遍历字符串数组序号0后面的数组元素,如果后面的数组元素的长度大于max.Length,则将其赋值给max.

  static void Main(string[] args)
        {
            string[] strs = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            Console.WriteLine("最长的字符串为:{0}", GetMaxLength(strs));
            Console.ReadKey();

        }
        static string GetMaxLength(string[] strs)
        {
            string max = strs[0];
            for (int i = 1; i < strs.Length;i++ )
            {
                if (strs[i].Length>max.Length)
                {
                    max = strs[i];
                }
            }
            return max;
View Code
  • 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。

代码:两两比较,依次将大数往后移动。数组array的第一个数需要比较array.Length-1次,第二个数需要比较array.Length-1-1次,第三个需要比较array.lenth-1-2次 依次类推 需要比较array.Length-1趟(for int i=0.i<array.Length-1;i++),每趟比较array.Length-1-i次(for(int j=0i<araay.Lenth-i-1))

 static void Main(string[] args)
        {
            int[] array=new int[]{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            for (int i = 0; i < array.Length-1;i++ )
            {
                for (int j = 0; j < array.Length - 1 - i;j++ )
                {
                    if (array[j]>array[j+1])
                    {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
                for (int k = 0; k < array.Length;k++ )
                {
                    Console.Write(array[k]+",");
                }
                Console.ReadKey();
            }
View Code
  •  为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。

 代码:随机数Random类,random变量必须定义在for循环外部,因为for循环每次循环的时间间隔非常短,而random随机数的生成是模拟随机数,是根据电脑系统的时间计算出来的,所以,若是在循环内部,每次定义定义一个random对象的话,时间间隔很短,生成的随机数就相同数字的概率非常大。而定义在循环的外部的话,每次循环都是以一个random对象为基准,虽然是在差不多相同时间内生成的数字,但是出现相同数字的概率要小。好比多个人根据时间按照一定的规则计算一个数字,因为时间相同,规则相同,就会出现两个人计算的结果是一样的。而如果是一个人计算时,会考虑到时间是相同的,所以需要适当采用不同的规则来计算。

        static void Main(string[] args)
        {
            Random random = new Random();
            int[] scores = new int[30];
            int sum = 0;
            float average;
            for (int i = 0; i < 30;i++ )
            {
                scores[i] = random.Next(101);
                Console.Write("第{0}个同学的成绩为{1}
", i + 1, scores[i]);
                sum += scores[i];
            }
            average = sum / scores.Length;
            Console.WriteLine("10位同学的平均成绩为:{0}", average);
            Console.ReadKey();
        }
View Code
  •  求平均值,并四舍五入
int[] nums = {1,3,5,7,93,33,4,4,6,8,13 };
            int sum = 0;
            for (int i = 0; i <nums.Length; i++)
            {
                sum +=nums[i];
            }
            double dNum = sum * 1.0 / nums.Length;
            Console.WriteLine(dNum.ToString("0.00"));
            Console.WriteLine(dNum.ToString("f2"));
            Console.WriteLine(dNum.ToString("#.##"));
            //四舍五入Math静态类提供的方法
            decimal d = Math.Round((decimal)dNum, 2);
            Console.WriteLine(d);
            Console.ReadKey();
View Code
原文地址:https://www.cnblogs.com/tobecabbage/p/3518160.html