《.NET开发技术》实验02(计科)基础类与集合

 

问题 A: c#输出最大值、最小值和平均值(A)

题目描述

使用C#编写一个控制台应用。输入10个正整数存入数组中,输出最大值、最小值和平均值

输入

输入10个正整数

输出

最大值、最小值和平均值

样例输入

1
2
3
4
5
6
7
8
9
10

样例输出

10
1
5.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int max = -1,min=999999999;
            int avg = 0,num;
            for (int i = 0; i < 10;i++ )
            {
                num=Convert.ToInt32(Console.ReadLine());
                avg += num;
                max = Math.Max(max, num);
                min = Math.Min(min, num);
            }
            double ans = avg;
            ans /= 10;
            Console.WriteLine(max);
            Console.WriteLine(min);
            Console.WriteLine(ans);
            Console.ReadKey();
        }
    }
}

 

问题 B: c#输出最大值、最小值和平均值(B)。

时间限制: 1 Sec  内存限制: 128 MB
提交: 217  解决: 107
[提交][状态][讨论版][命题人:lyh]

题目描述

使用C#编写一个控制台应用。输入若干个正整数存入数组中(输入exit表示输入结束),输出最大值、最小值和平均值

输入

输入若干个正整数存入数组中

输出

输出最大值、最小值和平均值。 平均值保留两位小数。

样例输入

1
2
3
4
5
6
7
8
9
exit

样例输出

9
1
5.00

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int max = -1,min=999999999,num;
            int avg = 0;
            string str,exit="exit";
         //   ArrayList arr = new ArrayList();
            int count = 0;
            for (int i = 0; ;i++ )
            {
                str = Console.ReadLine();
                if (String.Compare(str,exit)==0)
                {
                    break;
                }
                num = Convert.ToInt32(str);
               // arr[i] = num;
                count++;
                avg += num;
                max = Math.Max(max, num);
                min = Math.Min(min, num);
            }
            double ans = avg;
            ans /= (double)count;
          //  Math.Round(ans, 2);
            Console.WriteLine(max);
            Console.WriteLine(min);
            Console.WriteLine(ans.ToString("#0.00"));//保留两位小数
            Console.ReadKey();
        }
    }
}

 

问题 C: C#提取文件名

题目描述

假设有一个字符串包含了文件名、扩展名和路径,如strFileName=@“D:\C#程序设计\实验3\MyFile.TXT”。请使用C#编写一个静态方法,该方法能够取出路径中的文件名“MyFile.TXT”。

输入

一个包含了文件名,扩展名和路径的字符串。

输出

字符串中的文件名。

样例输入

strFileName=@“D:\C#程序设计\实验3\MyFile.TXT”

样例输出

MyFile.TXT

提示

提示:使用string类的lastindexof和substring等方法实现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            //int i=0;
            str = Console.ReadLine();
            int n = str.LastIndexOf("\\");
            int m = str.LastIndexOf("TXT");
            int len=str.Length;
            string ans = str.Substring(n+1,m-n+2);

            Console.Write(ans);
            Console.ReadKey();
        }
    }
}

问题 D: C#解密出生日期

题目描述

使用C#编写一个静态方法。该方法能够根据出生日期,(1)计算此人的年龄;(2)计算从现在到其60周岁期间,总共多少天。

输入

一个人的出生日期;

输出

第一行,此人的年龄(只按年度计算)
第二行,此人从现在到其60周岁期间,总共多少天(天数占5位宽度,右对齐)。

样例输入

2019-12-4

样例输出

0
21914

提示

假定现在的日期是2019年12月 5日

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int days;
            string str_now;
            TimeSpan span;
            string str = Console.ReadLine();
            DateTime dt1 = Convert.ToDateTime(str);//出生日期


           // string year = DateTime.Now.Year.ToString();
           // string month = DateTime.Now.Month.ToString();
           // string day = DateTime.Now.Day.ToString();
           // str_now = year + "-" + month + "-" + day;
           
            str_now = "2019-12-4";
            DateTime dt3 = Convert.ToDateTime(str_now);//现在的时间
            span = dt3.Subtract(dt1);
            int old = (span.Days - 1) / 365;
            Console.WriteLine(old);

            str=dt1.AddYears(60).ToString();
            DateTime dt2 = Convert.ToDateTime(str);//60周岁的时间

            span = dt2.Subtract(dt3);
            days = span.Days-1;
            Console.WriteLine(days);


            Console.ReadKey();
        }
    }
}

 

问题 E: C#判断回文字符串

题目描述

使用C#编写一个静态方法。该方法能够判断字符串是否是“回文”(即顺读和逆读相同的字符串)。

输入

一个字符串;

输出

如果是回文字符串,则输出“yes”,否则输出“no”;

样例输入

abcdcab

样例输出

no

提示

(1)用string类的toCahrArray()方法,将字符串转换为字符数组。(2)使用StringBuilder类保存逆序后的字符串。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            int i=0;
            str = Console.ReadLine();
            str.ToCharArray();
            int len = str.Length, flag = 1 ;
            for (i = 0; 2*i < len;i++ )
            {
                if(str[i]!=str[len-i-1])
                {
                    Console.WriteLine("no");
                    flag = 0;
                    break;
                }
            }
            if(flag==1)
                Console.WriteLine("yes");
            Console.ReadKey();
        }
    }
}

问题 F: 猜数(C#)

题目描述

编写一个控制台程序。以控制台方式输入整数,且调用Class1类CompareNum方法判断是否猜中,给出大了、小了、猜中三种提示。输入exit表示输入结束。

输入

输出

太小了
太大了
猜中了

提示

若输入的既不是数字,又不是exit,应给出合理提示。如请输入数字!

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            class1 r = new class1();
            string exit = "exit";
            Random rd = new Random();
            int x = rd.Next(10, 100);//生产10-100的数x
            while(true)
            {
                int flag = 0;
                string s = Console.ReadLine();
                if(String.Compare(s,exit)==0)
                {
                    break;
                }
                s.ToArray();
                for(int i=0;i<s.Length;i++)
                {
                    if(s[i]<'0'||s[i]>'9')
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag==1)
                {
                    Console.WriteLine("请输入数字!");
                    continue;
                }
                int y = Convert.ToInt32(s);//输入的数为y
                r.CompareNum(x,y);
            }
            
            Console.ReadKey();
        }
        class class1
        {
            public void CompareNum(int x,int y)
            {
                if (y > x)
                    Console.WriteLine("太大了");
                else if (y < x)
                    Console.WriteLine("太小了");
                else
                    Console.WriteLine("猜中了");  
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值