C#编程练习

编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //血量>0,执行攻击。
            //假定初始血量为100;
            int x = 100;
            Console.WriteLine("您当前的血量是100。   如果您确定发起魔法攻击,请输入yes:");
            string  p = Convert.ToString (Console.ReadLine());
            if (p == "yes")
            {
                
                while (x>0)
                {
                    x -= 20;
                    Console.WriteLine("您发起了进攻!,当前血量为:{0}", x);
                }
                Console.WriteLine("      -您已阵亡-    ");
            }

            else
                Console.WriteLine("您没有发起攻击!");

            
            
        }
    }
}

程序运行结果如下:

 

 

编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //输入n,计算1+...+n
            Console.WriteLine("请输入n:");
            int n = Convert.ToInt32(Console.ReadLine());
            int i = 1,sum=0;
            while (i<=n)
            {

                sum += i;
                i++;
            }
            Console.WriteLine(@"1+2+...+n的值为:"+"{0}",sum);
            
        }
    }
}

运行程序结果如下:

 

 

编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //输出1~100中的所有偶数
            int i = 1;
            while (i<=100)
            {
                if (i % 2 == 0)
                    Console.Write("{0}        ", i);
                i++;
            }











            
        }
    }
}

 

程序运行结果如下:

 

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //输入n1~n2,输出n1~n2中的所有偶数
            Console.WriteLine("请输入n1和n2:");
            int n1 = Convert.ToInt32(Console.ReadLine());
            int n2 = Convert.ToInt32(Console.ReadLine());
            int i;
            if (n1 < n2)
            {
                Console.WriteLine("开始执行");
                i = n1;
                while (i <= n2)
                {
                    if (i % 2 == 0)

                        Console.Write("{0}   ", i);

                    i++;
                }

            }
            else
            {
                Console.WriteLine("开始执行");
                i = n2;
                while (i <= n2)
                {
                    if (i % 2 == 0)

                        Console.Write("{0}   ", i);

                    i++;
                }

            }








            
        }
    }
}

程序运行结果如下:

 

 

 

编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            int count = 0;//计数
            //输入奇数:3n+1   输入偶数:/2
            Console.WriteLine("请输入数值n:");
            int n = Convert.ToInt32(Console.ReadLine());

            while (n!=1)
            {
               
                //n为奇数时
                if (n%2!=0)
                {
                    n = 3 * n + 1;
                    count++;
                }

              //n为偶数时
              else
                {
                    n /= 2;

                    count++;

                }

            }

            Console.WriteLine(count);







            
        }
    }
}

程序运行如下:

 

 

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //2006年初始人数8w,每年增长0.25,哪一年到20w

            int year = 2006;
            double num =80000;

            while (num <200000)
            {
                num  *=1.25;
                year++;

            }

            Console.WriteLine(year);


            
        }
    }
}

程序运行结果如下:

 

编写代码如下:

代码运行结果如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //输入学生个数,每个学生的年龄。求平均年龄,保留到小数点后面2位
            Console.WriteLine("请输入学生的个数:");
            int num = Convert.ToInt32(Console.ReadLine());
            int i = 1;
            int   sum=0;
            double ave;
            while (i<=num)
            {
                Console.WriteLine("请输入第{0}个学生的年龄:", i);
                int l=Convert .ToInt32 (Console .ReadLine ());
                sum += l;
                i++;
            }
            //计算平均值
            ave = (1.0*sum/num) ;
            //把ave保留2位小数
            ave = ((int)(ave * 100))/100.0;
            Console.WriteLine("平均年龄是:" + ave);
            
        }
    }
}

 

 编写代码如下:

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

namespace _1
{
    class Program
    {
        static void Main(string[] args)
        {

            //输入整数n,输出1~n所有整数
            Console.WriteLine("请输入数值n:");
            int n = Convert.ToInt32(Console.ReadLine());
            int i = 1;
            while (i<=n )
            {
                Console.Write(i+" ");
                i++;
            }
            
        }
    }
}

代码运行结果如下:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换? 2. 用方法来实现:将上题封装一个方法来做,方法有两个参数分别为num1,num2,将num1与num2交换。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*) 3. 请用户输入一个字符串,计算字符串中的字符个数,并输出。 4. 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:使用可变参数,params)。 5. 用方法来实现:计算1-100之间的所有整数的和。 6. 用方法来实现:计算1-100之间的所有奇数的和。 7. 用方法来实现:判断一个给定的整数是否为“质数”。 8. 用方法来实现:计算1-100之间的所有质数(素数)的和。 9. 用方法来实现:有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。 10. 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。 11. 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round() 12. 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 13. 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。 14. 将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。 15. 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。效果如图:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值