int i=0;
while(i<1000)
{
Console.WriteLine("下次考试一定要细心");
i++;
}
1、输入全部人数,和每个人的分数,求全班人总成绩和平均成绩:
Console.WriteLine("请输入人数:");
int studentcount = Convert.ToInt32(Console.ReadLine());
int i = 0;
double score = 0;
double sum = 0;
while (i < studentcount)
{
Console.WriteLine("请输入第{0}个人成绩为:", i + 1);
score = Convert.ToDouble(Console.ReadLine());
sum += score;
i++;
}
Console.WriteLine("总成绩为:{0}", sum);
Console.WriteLine("平均成绩为:{0}", sum / studentcount);
string s="";
int sum=0;
int i=0;
int a = 0;
do
{
Console.WriteLine("输入数字:");
s = Console.ReadLine();
if (s != "end")
{
try
{
i = Convert.ToInt32(s);
sum = sum + i;
if (a < i)
{
a = i;
}
}
catch{
Console.WriteLine("输入错误,请输入数字或者end!");
}
}
} while (s != "end");
Console.WriteLine("上面输入的数字之和为{0},其中最大的为{1}",sum,a);
【练习】找出100-999间的水仙花数?
for (int i = 100; i <= 999; i++)
{
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if (i == a * a * a + b * b * b + c * c * c)
{
Console.WriteLine(i);
}
}
【乘法表】
正方形:
int sum = 0;
for (int i = 1; i <= 9; i++)
{
//外层循环控制行数 即i代表行号
for (int j = 1; j <= 9; j++)
{
//第i行的第j个式子
sum = i * j;
Console.Write("{0}*{1}={2:00} ", i, j, sum);
}
Console.WriteLine();
}
梯形:
int sum = 0;
for (int i = 1; i <= 9; i++)
{
//外层循环控制行数 即i代表行号
for (int j = 1; j <= i; j++)
{
//第i行的第j个式子
sum = i * j;
Console.Write("{0}*{1}={2:00} ", i, j, sum);
}
Console.WriteLine();
}
例子:
int number = 0;
bool isNumber=false;
while (isNumber == false)
{
try
{
number = Convert.ToInt32(Console.ReadLine());
isNumber = true;
}
catch
{
Console.WriteLine("你刚刚输入的不是数字!");
isNumber = false;
}
}
for (int i = 0; i < number+1; i++)
{
Console.WriteLine("{0}+{1}={2}",i,number-i,number);