C#小练习08

来源:互联网 发布:新淘宝店铺转让价格表 编辑:程序博客网 时间:2024/05/22 16:53

1一维数组二维数组字符串中整数最大值及求和

1、形参为一维整型数组,求出一维数组元素的和以及最大值。

2、形参为二维整型数组,求出二维数组元素的和以及最大值。

3、输入字符串,例如“abcd17md358fy7dfd”,求出其中整数数值的和以及最大值。

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace CSharp_8_1{   class Program    {        static void OneDimension(int[] a)       {           int sum = 0;           int Max = 0;           for (int i = 0; i < a.Length; i++)           {                sum += a[i];                if (Max < a[i])                    Max = a[i];                 }           Console.WriteLine("一维数组元素的和为:{0},最大值为:{1}",sum,Max);       }       static void TwoDimension(int[,] a)       {           int sum = 0;           int Max = 0;           for (int i = 0; i < a.GetLength(0); i++)           {                for (int j =0; j < a.GetLength(1);j++)                {                    sum += a[i,j];                    if (Max < a[i,j])                        Max = a[i,j];                }           }           Console.WriteLine("二维数组元素的和为:{0},最大值为:{1}",sum, Max);       }        static void InPutCharacter()       {           int[] a = new int[10] {0,0,0,0, 0, 0, 0, 0, 0, 0 };           int num,flag =0,max=0,sum=0;            string s;            char[] sStr;            Console.WriteLine("输入一串字符");            s = Console.ReadLine();           sStr= s.ToCharArray();            num = s.Length;            while (num > 0)            {                int rem=1;                if (s[num - 1] <= '9'&& s[num - 1]>='0')                   {                    a[flag] = (int)s[num - 1] -'0';                                      while (s[num - rem-1] <='9' && s[num - rem-1] >= '0')                    {                        a[flag] +=(int)Math.Pow(10, rem) * ((int)s[num - rem - 1] - '0');                        rem++;                    }                    flag++;                   }              num-=rem ;            }   for (int i = 0; i < a.Length; i++)            {                sum += a[i];                if (a[i] > max)                    max = a[i];            }  Console.WriteLine("字符串中最大的数为:{0} 整数的和:{1}",max,sum);       }       static void Main(string[] args)       {           int[] a = new int[10] { 25,45,65,55,47,54,88,45,75,15};           int[,] b = new int[3,3] {{25,56,45},{45,26,15},{78,51,26} };           OneDimension(a);           TwoDimension(b);           InPutCharacter();       }    }}

2.时间类打印函数增加时间函数

定义一个时间类,该类包含时、分、秒字段与属性,并具有将时间增加n秒,n分,n时的方法,具有打印显示时、分、秒的方法。

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace CSharp_8_2{    class Time    {       static private int Hour = 12;       static private int Minute = 35;       static private int Second = 20;       static void Add()       {           int h, m, s;           string str;           Console.WriteLine("请输入形如:2:3:4 进行增加时:分:秒");           str = Console.ReadLine();           try           {//格式控制                string[] sArray = str.Split(newchar[] { ':' });                Hour += int.Parse(sArray[0]);                Minute += int.Parse(sArray[1]);                Second += int.Parse(sArray[2]);                Second = Second % 60;//保证 时分秒 进位正确 显示正确                s = Second / 60;                Minute = (Minute + s) % 60;                m = (Minute + s) / 60;                Hour = (Hour + m) % 24;                Print(1);           }           catch (Exception ex)           {                Console.WriteLine("输入格式不正确请重新运行");                Console.WriteLine(ex.Message);           }       }       static void Print(int flag)       {           if (flag == 1)           {                Console.WriteLine("时间重置为:{0}:{1}:{2}", Hour,Minute, Second);           }           else                Console.WriteLine("默认时间为:{0}:{1}:{2}", Hour,Minute, Second);       }       static void Main(string[] args)       {           Print(0);//打印时间           Add();//增加时间       }      } }

3.四名学生两门成绩统计平均成绩及不合格成绩

假设一个班有4名学生,学习两门课程,编写程序实现如下功能:

(1)调用函数Input(),输入者4个学生的学号和成绩;

(2) 调用函数Average(),返回者4个学生2门课的平均成绩,在主函数中输出;

(3)调用函数NoPass(),返回所有不及格的学生的学号,在主函数中输出;

using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace CSharp_8_3{   class Program    {       static void Input(int[,] a, int[] b)       {                      Console.WriteLine("请输入四名学生的学号以空格隔开");           string s=Console.ReadLine();            string[] str = s.Split(new char[]{' '});           Console.WriteLine("四名学生的学号分别为:");           for (int i = 0; i < b.Length; i++)           {                b[i] = int.Parse(str[i]);           }           foreach (var item in str)           {                Console.Write("{0,4}",item);           }           Console.WriteLine("\n请输入第一门课的成绩:(空格隔开)");           string first = Console.ReadLine();           string[] firststr = first.Split(new char[] { ' ' });           for (int i = 0; i < a.GetLength(1); i++)           {                a[0,i] =int.Parse(firststr[i]);           }           Console.WriteLine("\n请输入第二门课的成绩:(空格隔开)");           string second = Console.ReadLine();           string[] secondstr =second.Split(new char[] { ' ' });           for (int i = 0; i < a.GetLength(1); i++)           {                a[1, i] =int.Parse(secondstr[i]);           }           Console.WriteLine("恭喜你 成绩录入成功!");                           }       static void Average(int[,] a,int[] b)       {           for (int i = 0; i < b.Length; i++)           {                Console.WriteLine("\n学生学号为:{0,4}的同学的两门课平均成绩为:{1,4}",b[i],1.0*(a[0,i]+a[1,i])/2);           }       }        static void NoPass(int[,] a, int[] b)       {           for (int i = 0; i < a.GetLength(0); i++)           {                for (int j = 0; j <a.GetLength(1); j++)                {                    if (a[i,j]<60)                    {                       Console.WriteLine("学号为{0,10}的同学成绩不及格",b[j]);                    }                }           }       }       static void Main(string[] args)       {           int[,] a = new int[2, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };           int[] b = new int[4] { 0, 0,0, 0 };           Input(a, b);//录入           Average(a, b);//平均值           NoPass(a, b);//判断       }    }}


 

 

0 0