1.九九乘法表
//for循环
for (int i = 1; i <= 9; i++)
{
for(int j = 1; j <=i; j++)
{
Console.Write("{0}*{1}={2}\t",i,j,i*j);
}
Console.WriteLine();
}
//while循环
int a = 1;
while (a<=9)
{
int b = 1;
while(b<=a)
{
Console.Write("{0}*{1}={2}\t", a, b, a * b);
b++;
}
Console.WriteLine();
a++;
}
Console.ReadKey();
2. 找出100以内与7有关的数并打印:
for (int i = 1; i < 100; i++)
{
if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0)
{
Console.WriteLine(i);
}
}
//while循环
int i = 1;
while (i < 100)
{
if(i%10 ==7 ||i/10 ==7 || i%7 == 0)
{
Console.WriteLine(i);
}
i++;
}
Console.ReadKey();
3.求一个数的阶乘
Console.WriteLine("请输入一个正整数:");
int a = int.Parse(Console.ReadLine());
long b = 1;
for (long i = 1; i <= a; i++)
{
b *= i;
}
Console.WriteLine("这个数的乘积是:"+b);
//while循环
int num;
do
{
string count = Console.ReadLine();
if(int.TryParse(count,out num) && num > 0)
{
break;
}
else
{
Console.WriteLine("请输入一个正确的正整数:");
}
} while (true);
long product = 1;
long j =1;
do
{
product *= j;
j++;
} while (j<=num);
Console.WriteLine("这个数的乘积是:" + product);
Console.ReadKey();
}
4.、一个游戏,前20关是每一关自身的分数,,21-30关每一关是10分,31-40关,每一关是20分,41-49关,每一关是30分,50关,是100分,输入你现在闯到的关卡数,求你现在拥有的分数。
int num = 0;
Console.WriteLine("请输入你的闯关卡数:");
int a = int.Parse(Console.ReadLine());
if (a >= 1 && a <= 50)
{
for (int i = 0; i < a; i++)
{
if (i <= 20)
{
num += i;
}
else if (i >= 21 && i <= 30)
{
num += 10;
}
else if (i >= 31 && i <= 40)
{
num += 20;
}
else if (i >= 41 && i <= 49)
{
num += 30;
}
else
{
num += 100;
}
}
}
Console.WriteLine("你的分数为:" + num);
int stage;
int score = 0;
//do...while循环
do
{
Console.WriteLine("请输入你现在闯到的关卡数:");
string input = Console.ReadLine();
if(int.TryParse(input,out stage) && stage >= 1 && stage <= 50)
{
if(stage <= 20)
{
score =(stage*(stage + 1))/2;
}
else if(stage <= 30)
{
score = 210 + (stage - 20) * 10;
}
else if (stage <= 40)
{
score =410 +(stage - 30) * 20;
}
else if (stage <= 49)
{
score =810 + (stage -40) * 30;
}
else
{
score = 990 + 100;
}
break;
}
else
{
Console.WriteLine("输入有误!请输入1-50之间的整数");
}
} while (true);
Console.WriteLine($"你现在的分数为:{score}");
Console.ReadKey();
}
5.篮球弹起问题,篮球初始所在高度是20米,每次弹起是上一次的五分之四,输入次数,求篮球弹起高度。
Console.WriteLine("请输入弹起的次数:");
int c = int.Parse(Console.ReadLine());
double hight = 20;
double hig = 0;
while (hig < c)
{
hight = hight * 4 / 5;
hig++;
}
Console.WriteLine("经过{0}次弹起后,篮球的高度为:{1}", c, hight);
//for循环
Console.WriteLine("请输入次数:");
int a = int.Parse(Console.ReadLine());
double h = 20;
for (int i = 0; i < a; i++)
{
h = h * 4 / 5;
}
Console.WriteLine("经过{0}次弹起,篮球高度是:{1}",a,h);
6.有一张超大的纸,可以无限次折叠,厚度为0.07毫米。问,折叠多少次可以超过珠峰的高度(8848米)。
int cishu = 0;
double h = 0.07;
for (; ; )
{
cishu++;
h *= 2;
if (h > 8848000)
{
break;
}
}
Console.WriteLine("经过{0}次折叠后,可以超过珠峰的高度,高度为:{1}米", cishu, h / 1000);
//while循环
int cishu1 = 0;
double h1 = 0.07;
while (h < 8848000)
{
h1 *= 2;
cishu1++;
}
Console.WriteLine("经过{0}次折叠,可以超过珠峰的高度,高度为{1}米",cishu1 ,h1/1000);
7.输入一个整数,求1!+2!+...+n!
Console.WriteLine("请输入一个整数:");
int a =int.Parse(Console.ReadLine());
int sum = 0;
int jiecheng = 1;
for (int i =1;i<= a ;i++ )
{
jiecheng *= i;
sum += jiecheng;
}
Console.WriteLine("结果是:"+sum);
//while循环
Console.WriteLine("请输入一个整数:");
int s =int.Parse(Console.ReadLine());
int jiecheng1 = 1;
int sum1 = 0;
int k =1;
while(k <= s)
{
jiecheng1 *= k;
sum1 += jiecheng1;
k++;
}
Console.WriteLine("结果是:"+sum1);
8.用*打印直角在左上方的三角形
Console.WriteLine("请输入你要打印多大的三角形:");
int a2 =int.Parse(Console.ReadLine());
for (int i =0;i<= a2 ; i++)
{
for (int j =1;j<= a2-i ; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
//while循环
int i1 = 0;
while (i1 <= a2)
{
int j1 = 1;
while (j1 <= a2 -i1)
{
Console.Write("*");
j1++;
}
Console.WriteLine();
i1++;
}
9.用*打印直角在左下方的三角形
Console.WriteLine("请输入三角形最大行*的个数:");
int a =int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
//while循环
int i1 = 1;
while (i1<=a)
{
int j1 = 1;
while (j1 <= i1)
{
Console.Write("*");
j1++;
}
Console.WriteLine();
i1++;
}
10.用*打印直角在右上方的三角形
Console.WriteLine("请输入你要打印的三角形最大行*的个数");
int a =int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
for (int j = 0; j <= i-1; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= a-i +1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
//while循环
int i1 = 1;
while (i1 <= a)
{
int j1 = 0;
while (j1 <= i1-1)
{
Console.Write(" ");
j1++;
}
int k1 = 1;
while (k1 <= a-i1 +1)
{
Console.Write("*");
k1++;
}
Console.WriteLine();
i1++;
}
11.用*打印直角在右下方的三角形
Console.WriteLine("请输入你要打印的三角形最大行*的个数");
int a =int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <=a- i+1; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
//while循环
int i1 = 1;
while (i1 <= a)
{
int j1 = 1;
while(j1 <= a -i1+1)
{
Console.Write(" ");
j1++;
}
int k1 = 1;
while(k1 <= i1)
{
Console.Write("*");
k1++;
}
Console.WriteLine();
i1++;
}
12.打印菱形
Console.WriteLine("请输入一个数:");
int a = int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <=a- i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <=2*i -1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
for(int i = 1;i <= a; i++)
{
for(int j = 1;j <=i; j++)
{
Console.Write(" ");
}
for( int k = 1; k <= 2 *(a-i) -1;k++)
{
Console.Write("*");
}
Console.WriteLine();
}
13.输入任意数,打印出对应三角形
Start:
Console.WriteLine("请输入一个数:");
int a = int.Parse(Console.ReadLine());
if (a % 2 == 0)
{
Console.WriteLine("输入的数字必须为奇数");
goto Start;
}
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= a - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= 2 * (a - i) - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
//用while循环
Start:
Console.WriteLine("请输入菱形最长行*的个数:");
int a = int.Parse(Console.ReadLine());
if (a % 2 == 0)
{
Console.WriteLine("输入的数字必须为奇数");
goto Start;
}
int mid = a / 2 + 1; // 中间行的行号
int i5 = 1; // 当前行号
// 打印上半部分(包括中间行)
while (i5 <= mid)
{
int spaces = mid - i5; // 计算前导空格数
int stars = 2 * i5 - 1; // 计算星号数
PrintSpacesAndStars(spaces, stars);
Console.WriteLine();
i5++;
}
// 打印下半部分
while (i5 <= a)
{
int spaces = i5 - mid; // 计算前导空格数
int stars = 2 * (a - i5) + 1; // 计算星号数
PrintSpacesAndStars(spaces, stars);
Console.WriteLine();
i5++;
}
Console.ReadKey();
}
static void PrintSpacesAndStars(int spaces, int stars)
{
int pos = 0;
while (pos < spaces)
{
Console.Write(" ");
pos++;
}
pos = 0;
while (pos < stars)
{
Console.Write("*");
pos++;
}
}
14.输入任意数,打印出对应的等腰三角形
Console.WriteLine("请输入一个数字:");
int a = int.Parse(Console.ReadLine());
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= a - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
//while循环
Console.WriteLine("请输入一个数字:");
int a =int.Parse(Console.ReadLine());
int i = 1;
while (i <=a)
{
int j = 1;
while(j <=a-i)
{
Console.Write(" ");
j++;
}
int k = 1;
while(k <= 2*i -1)
{
Console.Write("*");
k++;
}
Console.WriteLine();
i++;
}
标签:练习题,Console,int,Parse,while,循环,WriteLine,ReadLine,输入
From: https://blog.csdn.net/2302_82303876/article/details/140890339