流程控制練習(xí):
// 1-兩數(shù)的加減乘除
Console.WriteLine("請(qǐng)輸入兩個(gè)整數(shù)并回車:");
int i1 = Convert.ToInt32(Console.ReadLine());
int i2 = Convert.ToInt32(Console.ReadLine());
int jia, jian, cheng;
float chu;
Console.WriteLine("請(qǐng)輸入運(yùn)算:0-加法,1-減法接奈,2-乘法,3-除法");
int y = Convert.ToInt32(Console.ReadLine());
switch (y)
{
case 0:
jia = i1 + i2;
Console.WriteLine("兩數(shù)相加得數(shù)是:" + jia);
break;
case 1:
jian = i1 - i2;
Console.WriteLine("兩數(shù)相減得數(shù)是:" + jian);
break;
case 2:
cheng = i1 * i2;
Console.WriteLine("兩數(shù)相乘得數(shù)是:" + cheng);
break;
case 3:
chu = (float)i1 / i2;
Console.WriteLine("兩數(shù)相除得數(shù)是:" + chu.ToString("#0.00"));
break;
default:
Console.WriteLine("輸入有誤什乙!");
break;
}
2-求出1~1000之間的所有能被7整除的數(shù),并計(jì)算和輸出每5個(gè)的和抄谐。
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個(gè)數(shù)的和是:"+sum);
sum = 0;
count = 0;
}
}
3-分別輸出1~100之間的平方导狡、平方根纹坐。
int pf = 1;
double pfg;
Console.WriteLine("100以內(nèi)的數(shù)的平方:");
for (int i = 1; i <= 100; i++)
{
pf = i * i;
Console.WriteLine(pf);
}
Console.WriteLine("100以內(nèi)的數(shù)的平方根:");
for (int j = 1; j <=100; j++)
{
pfg = Math.Sqrt(j);
Console.WriteLine(pfg);
}
Console.WriteLine(d);
4-100中能被3整除但不能被5整除的數(shù),并統(tǒng)計(jì)有多少個(gè)這樣的數(shù)归斤。
int count = 0;
for (int i = 0; i <= 100; i++)
{
if(i%3==0 && i%5!=0)
{
Console.WriteLine(i);
count++;
}
}
Console.WriteLine("100以內(nèi)能被3整除不能被5整除的數(shù)及個(gè)數(shù):"+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個(gè)大寫字母痊夭,如果用戶輸入的信息不滿足要求,提示幫助信息并要求重新輸入官册。
// 1.獲取用戶輸入
Console.WriteLine("請(qǐng)輸入5個(gè)大寫字母:");
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("輸入準(zhǔn)確");
}
}
Console.WriteLine(strs[i]);
}
7-四個(gè)數(shù)比較取最大最小值
//Console.WriteLine("請(qǐng)輸入四個(gè)數(shù):");
//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)
{
// 定義一個(gè)變量來(lá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將大的數(shù)字移到數(shù)組的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將大的數(shù)字移到數(shù)組的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;
}
}
}
*/
面向?qū)ο?/h4>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者