编写代码如下:
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++;
}
}
}
}
代码运行结果如下: