namespace _09.do_while循环练习01
{
class Program
{
static void Main(string[] args)
{
//计算1到100之间的整数的和;
int limit =100;
int sum =0;
int i = 1;
do
{
sum += i;
i++;
} while (i<=limit);
Console.WriteLine("1到100之间的和整数和为:{0}.",sum);
Console.ReadKey();
}
}
}
练习2:
namespace _10.do_while循环练习02
{
class Program
{
static void Main(string[] args)
{
//要求用户输入用户名和密码,只要不是admin,888888就一直提示用户名或密码错误,请重新输入.
bool b = true; //b用来控制循环是否继续下去
do
{
Console.WriteLine("请输入用户名:");
string name = Console.ReadLine();
Console.WriteLine("请输入密码:");
string pwd = Console.ReadLine();
if (name == "admin" && pwd == "888888")
{
Console.WriteLine("输入很正确");
b = false;
}
else
{
Console.WriteLine("用户名或密码错误请重新输入!");
}
} while (b);
Console.ReadKey();
}
}
}
练习3:
namespace _11.do_while循环练习03
{
class Program
{
static void Main(string[] args)
{
//不断要求用户输入学生姓名,输入q结束循环.
string name;//用来保存输入的姓名
do
{
Console.WriteLine("请输入学生姓名:");
name = Console.ReadLine();
} while (name!="q");
Console.ReadKey();
}
}
}
练习4:
namespace _12.do_while循环04
{
class Program
{
static void Main(string[] args)
{
//不断要求用户输入一个数字,然后打印这个数字的两倍,输入q结束循环.
int number=0;
string s = null; //用来验证是否输入了"q"
do
{
Console.WriteLine("请输入一个数字:");
s = Console.ReadLine();
if (s == "q")
{
break;
}
else
{
try
{
Console.WriteLine(int.Parse(s)*2);
}
catch
{
Console.WriteLine("输入有误.");
}
}
} while (s != "q");
Console.ReadKey();
}
}
}
练习5:
namespace _13.do_while循环05
{
class Program
{
static void Main(string[] args)
{
//不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候,
//显示共输入的数字中的最大值
int number = 0; //用来保存输入的整数
int temp = 0; //用来保存最大的数
string s = null; //用来保存输入的内容
do
{
Console.WriteLine("请输入一个整数");
try
{
s = Console.ReadLine();
number = Convert.ToInt32(s);
if (number > temp)
{
temp = number;
}
}
catch
{
if (s == "end")
{
Console.WriteLine(temp);
break;
}
}
} while (true);
Console.ReadKey();
}
}
}