流程控制练习:
// 1-两数的加减乘除
Console.WriteLine("请输入两个整数并回车:");
int i1 = Convert.ToInt32(Console.ReadLine());
int i2 = Convert.ToInt32(Console.ReadLine());
int jia, jian, cheng;
float chu;
Console.WriteLine("请输入运算:0-加法,1-减法,2-乘法,3-除法");
int y = Convert.ToInt32(Console.ReadLine());
switch (y)
{
case 0:
jia = i1 + i2;
Console.WriteLine("两数相加得数是:" + jia);
break;
case 1:
jian = i1 - i2;
Console.WriteLine("两数相减得数是:" + jian);
break;
case 2:
cheng = i1 * i2;
Console.WriteLine("两数相乘得数是:" + cheng);
break;
case 3:
chu = (float)i1 / i2;
Console.WriteLine("两数相除得数是:" + chu.ToString("#0.00"));
break;
default:
Console.WriteLine("输入有误!");
break;
}
2-求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
int sum = 0, count=0;
for (int i = 1; i < 1000; i++)
{
if (i % 7 == 0)
{
sum += i;
count++;
Console.WriteLine(i);
}
if(count==5)
{
Console.WriteLine("这5个数的和是:"+sum);
sum = 0;
count = 0;
}
}
3-分别输出1~100之间的平方、平方根。
int pf = 1;
double pfg;
Console.WriteLine("100以内的数的平方:");
for (int i = 1; i <= 100; i++)
{
pf = i * i;
Console.WriteLine(pf);
}
Console.WriteLine("100以内的数的平方根:");
for (int j = 1; j <=100; j++)
{
pfg = Math.Sqrt(j);
Console.WriteLine(pfg);
}
Console.WriteLine(d);
4-100中能被3整除但不能被5整除的数,并统计有多少个这样的数。
int count = 0;
for (int i = 0; i <= 100; i++)
{
if(i%3==0 && i%5!=0)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine("100以内能被3整除不能被5整除的数及个数:"+count);
5-九九乘法表
int j = 1;
for (int r = 1; r <= 9; r++)
{
for (int c = 1; c < 10; c++)
{
if(r>=c)
Console.Write("{0}*{1}={2}\t", c, r, r * c);
}
Console.WriteLine();
}
6- 要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。
// 1.获取用户输入
Console.WriteLine("请输入5个大写字母:");
string strs = Console.ReadLine();
for (int i = 0; i < strs.Length; i++)
{
if (strs[i] >= 'A' && strs[i] <= 'Z')
{
Console.WriteLine("输入的是大写");
}
foreach (char cha in strs)
{
if (cha <= 122 && cha >= 97)
{
Console.WriteLine("输入准确");
}
}
Console.WriteLine(strs[i]);
}
7-四个数比较取最大最小值
//Console.WriteLine("请输入四个数:");
//int n1 = Convert.ToInt32(Console.ReadLine());
//int n2 = Convert.ToInt32(Console.ReadLine());
//int n3 = Convert.ToInt32(Console.ReadLine());
//int n4 = Convert.ToInt32(Console.ReadLine());
// int[] numbers = { 6, 5, 19, 14 }; // 0 ,1,2,3
int max = 0;
int min = 0;
/*
Array.Sort(numbers);
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
min = numbers[0];
max = numbers[3];
Console.WriteLine(max+min);
*/
/*
//max = (n1 > n2 ? n1 : n2) > (n3 > n4 ? n3 : n4) ? (n1 > n2 ? n1 : n2) : (n3 > n4 ? n3 : n4);
//min = (n1 < n2 ? n1 : n2) < (n3 < n4 ? n3 : n4) ? (n1 < n2 ? n1 : n2) : (n3 < n4 ? n3 : n4);
//Console.WriteLine(max);
//Console.WriteLine(min);
*/
/*
for (int i = 1; i < numbers.Length; ++i)
{
// 定义一个变量来接受numbers[1]
int t = numbers[i];
// 用j记录索引
int j = i; // 第一次: 1
// j>0 && numbers[0] > numbers[1]
// 递减排序,每一次都是后一位比前一位,拿到最大值
while ((j > 0) && (numbers[j - 1] > t))
{
// numbers[1] = numbers[0]
numbers[j] = numbers[j - 1];
--j;// 先减
}
numbers[j] = t;
}
*/
/*
int[] numbers = { 6, 5, 19, 14 };
// i < 3
for (int i = 0; i < numbers.Length - 1; i++)
{
Console.WriteLine(numbers[i]);
// # region将大的数字移到数组的arr.Length-1-i
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
max = numbers[j + 1];
numbers[j + 1] = numbers[j];
numbers[j] = max;
}
}
}
*/
/*
int[] numbers = { 6, 5, 19, 14 };
// i < 3
for (int i = 3; i >= 0; i--)
{
Console.WriteLine(numbers[i]);
// # region将大的数字移到数组的arr.Length-1-i
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
max = numbers[j + 1];
numbers[j + 1] = numbers[j];
numbers[j] = max;
}
}
}
*/
面向对象