作者:劉一哥GIS(CSDN博客專家)
博客地址:https://geostorm.blog.csdn.net/
劉一哥,多年研究地圖學、地理資訊系統、遙感、攝影測量和GPS等應用,精通ArcGIS、MapGIS、ENVI、Erdas、CASS、Pix4d、CC、PhotoScan、Inpho、EPS、Globalmapper等專業軟體的應用,精通多門編程語言,擅長GIS二次開發和資料庫系統開發,具有豐富的行業經驗,致力于測繪、地信、國土、資源、環境、生態、空間規劃、無人機等領域深度應用,
C#經典案例合集:
1. 編一個程式,定義常量Pi=3.14159265,從鍵盤上輸入半徑r,求出圓的面積,
const double Pi = 3.14159265;
double r,s;
Console.WriteLine("請輸入圓的半徑");
r = double.Parse(Console.ReadLine());
s = Pi * r * r;
Console.WriteLine("圓的面積是{0}",s);
2. 編一個程式,定義三個double型別的變數,分別從鍵盤上輸入值給它們,然后用Console.WriteLine方法把它們輸出成一列,小數點對齊,保留3位小數,
double x, y, z;
Console.WriteLine("請輸入一個數:");
x = double.Parse(Console.ReadLine());
Console.WriteLine("請輸入一個數:");
y = double.Parse(Console.ReadLine());
Console.WriteLine("請輸入一個數:");
z = double.Parse(Console.ReadLine());
Console.WriteLine("{0,10:f3}", x);
Console.WriteLine("{0,10:f3}", y);
Console.WriteLine("{0,10:f3}", z);
//Console.WriteLine("{0:f3}", x);
//Console.WriteLine("{0:f3}", y);
//Console.WriteLine("{0:f3}", z);
3. 編一個程式,從鍵盤上輸入三個數,用三元運算子(? :)把最大數找出來,
float x, y, z, temp;
Console.Write("請輸入一個實數:");
x = float.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
y = float.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
z = float.Parse(Console.ReadLine());
temp = x >= y ? x : y;
temp = temp >= z ? temp : z;
Console.WriteLine("最大數為:{0}", temp);
4. 編一個程式,從鍵盤上輸入三個數,用三元運算子(? :)把最小數找出來,
float x, y, z, temp;
Console.Write("請輸入一個實數:");
x = float.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
y = float.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
z = float.Parse(Console.ReadLine());
temp = x <= y ? x : y;
temp = temp <= z ? temp : z;
Console.WriteLine("最大數為:{0}", temp);
5.編一個程式,輸入一個字符,如果是大寫字母,就轉換成小寫字母,否則不轉換,
char ch;
Console.WriteLine("請輸入一個字符");
ch = char.Parse(Console.ReadLine());
if (ch >= 'A' && ch <= 'Z')
{
ch = (char)(ch + 32);
}
Console.WriteLine(ch);
6.輸入一個字符,判定它是什么型別的字符(大寫字母,小寫字母,數字或者其它字符)
char ch;
Console.WriteLine("請輸入一個字符");
ch = char.Parse(Console.ReadLine());
if (ch >= 'A' && ch <= 'Z')
Console.WriteLine("大寫字母");
else if (ch >= 'a' && ch <= 'z')
Console.WriteLine("小寫字母");
else if (ch >= '0' && ch <= '9')
Console.WriteLine("數字");
else
Console.WriteLine("其他字母");
7.編一個程式,設圓的半徑r=1.2,高h=1.5,定義圓周率常量Pi=3.1415,求出圓柱的體積,
const double Pi = 3.1415;
double r=1.2,h=1.5;
double v;
v = Pi * r * r * h;
Console.WriteLine("圓柱體的體積是{0}",v);
8. (20)編一個程式,設園半徑r=1.5,園柱高h=3.6,圓周率3.1415定義為常量,求出圓周長、圓面積、圓柱體積,然后用Console.WriteLine方法輸出計算結果,輸出時要求有文字說明,取小數點后2位數字,例如,圓周長=×××.××,
const double Pi = 3.1415;
double r = 3, h = 5;
double c, s, v;
c = 2 * Pi * r;
s = Pi * r * r;
v = Pi * r * r * h;
Console.WriteLine("圓的周長為{0:f2}",c);
Console.WriteLine("圓的面積為{0:f2}", s);
Console.WriteLine("圓的體積為{0:f2}", v);
9.編一個程式,輸入一個字符,如果輸入的字符是大寫字母,則轉換為小寫字母;如果輸入的字符是小寫字母,則轉換為大寫字母,否則不轉換,
char ch,c;
Console.WriteLine("請輸入一個字符");
ch = char.Parse(Console.ReadLine());
if (ch >= 'A' && ch <= 'Z')
{
c = (char)(ch + 32);
Console.WriteLine("字符{0}為大寫字母,轉換為小寫字母為{1}", ch, c);
}
else if (ch >= 'a' && ch <= 'z')
{
c = (char)(ch - 32);
Console.WriteLine("字符{0}為小寫字母,轉換為大寫字母為{1}", ch, c);
}
else
Console.WriteLine("{0}既不是大寫字母也不是小寫字母",ch);
10.編一個程式,定義結構型別(有學號、姓名、性別和程式設計成績四個欄位),宣告該結構型別變數,用賦值陳述句對該變數賦值以后再輸出,
struct student
{
public int no;
public string name;
public string sex;
public double score;
}
class Program
{
static void Main(string[] args)
{
student stu;
stu.no = 10003;
stu.name = "小小";
stu.sex = "女";
stu.score = 89.5;
Console.WriteLine("學生的學號是{0},名字叫{1},性別是{2},程式設計的成績是{3}",stu.no,stu.name,stu.sex,stu.score);
}
}
11. 編一個程式,定義一個實數變數,從鍵盤上輸入一個值,如果這個值在閉區間[0,100]里,則加上1000,否則不加,最后輸出結果,
float f,g;
Console.WriteLine("請輸入一個實數變數");
f = float.Parse(Console.ReadLine());
if (f >= 0 && f <= 100)
{
g = f + 1000;
Console.WriteLine("{0}在0-100之間,最后結果為{1}", f, g);
}
else
Console.WriteLine("{0}不在0-100之間",f);
12. 編一個程式,輸入一個整數,使用if else陳述句,如果該數大于0,則加上100,否則加上500,輸出結果,
int i,j;
Console.WriteLine("請輸入一個整數");
i = int.Parse(Console.ReadLine());
if (i > 0)
{
j = i + 100;
Console.WriteLine("{0}>0,最后結果是{1}", i, j);
}
else
{
j = i + 500;
Console.WriteLine("{0}<0,最后結果是{1}",i,j);
}
13. 編一個程式,用if else陳述句,輸入一個整數,如果該數大于0,則輸出"這個數大于零,",如果該數等于0,則輸出"這個數等于零,",否則輸出"這個數小于零,",
int i;
Console.WriteLine("請輸入一個整數");
i = int.Parse(Console.ReadLine());
if (i > 0)
{
Console.WriteLine("這個數大于0");
}
else if(i==0)
{
Console.WriteLine("這個數等于0");
}
else
{
Console.WriteLine("這個數小于0");
}
14.編一個程式,輸入一個正數,對該數進行四舍五入到個位數的運算,例如,實數12.56經過四舍五入運算,得到結果13;而12.46經過四舍五入運算,得到結果12,
double d;
int i,e;
Console.WriteLine("請輸入一個正數");
d = double.Parse(Console.ReadLine());
i=(int)d;
if ((d - i) >= 0.5)
e = i + 1;
else
e = i;
Console.WriteLine("{0}四舍五入的結果為{1}", d, e);
15. 撰寫一個程式,定義三個float型別的變數,分別從鍵盤上輸入值給它們, 然后用if else選擇陳述句找出它們中的最小數,最后輸出結果,
float x, y, z,min=0;
Console.WriteLine("請輸入三個float型別的值");
x = float.Parse(Console.ReadLine());
y = float.Parse(Console.ReadLine());
z = float.Parse(Console.ReadLine());
if (x < y && x < z)
min = x;
else if (y < x && y < z)
min = y;
else
min = z;
Console.WriteLine("這三個數中的最小值為{0}",min);
16. 編一個程式,輸入三個實數,要求使用if else陳述句把它們的中間數找出來,可以使用邏輯運算子,
float x, y, z, second = 0;
Console.WriteLine("請輸入三個float型別的值");
x = float.Parse(Console.ReadLine());
y = float.Parse(Console.ReadLine());
z = float.Parse(Console.ReadLine());
if ((x>y&&x<z)||(x<y&&x>z))
second = x;
else if ((y>x&&y<z)||(y<x&&y>z))
second = y;
else
second = z;
Console.WriteLine("這三個數中的中間數為{0}", second);
17.編一個程式,首先輸入一個成績(0到100的整數),用if else陳述句然后判斷該成績是優、良、中、及格還是不及格,
double score;
Console.WriteLine("請輸入一個成績");
score = double.Parse(Console.ReadLine());
if (score >= 90)
Console.WriteLine("優");
else if (score >= 80)
Console.WriteLine("良");
else if (score >= 70)
Console.WriteLine("中");
else if (score >= 60)
Console.WriteLine("及格");
else
Console.WriteLine("不及格");
18.編一個程式,輸入0—100之間的一個學生成績分數,用switch陳述句輸出成績等第(成績優秀(90-100),成績良好(80-89),成績及格(60-79)和成績不及格(59以下)),
int score;
Console.WriteLine("請輸入學生成績");
score = int.Parse(Console.ReadLine());
score = score / 10;
switch (score)
{
case 10:
Console.WriteLine("成績優秀");
break;
case 9:
Console.WriteLine("成績優秀");
break;
case 8:
Console.WriteLine("成績良好");
break;
case 7:
Console.WriteLine("成績及格");
break;
case 6:
Console.WriteLine("成績及格");
break;
default:
Console.WriteLine("成績不及格");
break;
}
19.編一個程式,利用do-while回圈陳述句,從鍵盤上輸入10個整數,求出它們的和,
int i=1,n,sum=0;
do
{
Console.WriteLine("請輸入一個整數");
n = int.Parse(Console.ReadLine());
sum = sum + n;
i++;
}while(i<=10);
Console.WriteLine("這十個整數的和為{0}",sum);
20. 編一個程式,用while回圈陳述句來計算1+1/2+2/3+3/4+…+99/100之和,
double i = 1,s=1;
while(i<=99)
{
s = s + i / (i + 1);
i++;
}
Console.WriteLine("和為{0}",s);
21. 編一個程式,用do-while回圈陳述句來計算PI=4(1-1/3+1/5+…+1/n),要求當1/n<0.000001時停止計算,*
double pi,s=0,x;
double n=1;
do
{
x = Math.Pow(-1,n + 1) / (2 * n - 1);
s = s + x;
n++;
} while (1 / n >= 0.000001);
pi = s * 4;
Console.WriteLine("pi的值為:{0}",pi);
22.編一個程式,從鍵盤上輸入二個實數,使用Math類中Max()和Min()方法,求出它們中的最大數和最小數,
double d1, d2,max,min;
Console.WriteLine("請輸入兩個實數");
d1 = double.Parse(Console.ReadLine());
d2 = double.Parse(Console.ReadLine());
max=Math.Max(d1,d2);
min = Math.Min(d1,d2);
Console.WriteLine("最大值為{0},最小值為{1}",max,min);
23.編一個程式,利用while回圈陳述句和Math類中的Pow()方法,求出1到10的各個數的平方之和,
double i = 1,s=0;
while (i <= 10)
{
s = s + Math.Pow(i, 2);
i++;
}
Console.WriteLine("和為{0}",s);
24. 編一個程式,列印出所有的“水仙花數”,所謂“水仙花數”是指一個三位數,其各位數字立方和等于該數本身,例如, 153=111+555+333,所以153是“水仙花數”,
int a, i, j, k;
for (a = 100; a <= 999; a++)
{
i = a % 10;
k = a / 100;
j = a % 100 / 10;
if (a == Math.Pow(i, 3) + Math.Pow(j, 3) + Math.Pow(k, 3))
Console.WriteLine("{0}是水仙花數",a);
}
25.撰寫一個程式,用while陳述句,求出1+(1+2)+(1+2+3)+…+(1+2+3+…+10)之和,
int i = 1, s = 0,j=1, s1 = 0;
while (j <=10)
{
while (i <= j)
{
s = s + i;
i++;
}
s1=s1+s;
j++;
}
Console.WriteLine("{0}", s1);
26.編一個程式,輸入二個正整數,求出它們的最大公約數,
int i, j,k;
Console.WriteLine("請輸入兩個正整數");
i = int.Parse(Console.ReadLine());
j = int.Parse(Console.ReadLine());
k = i < j ? i : j;
while (!(i % k == 0 && j % k == 0))
k--;
Console.WriteLine("最大公約數為{0}",k);
27.編一個程式,用while回圈陳述句,從鍵盤輸入10個數,要求找出最大數和次大數,
int i=1,max=1,max1=1;
int s;
Console.WriteLine("請輸入10個數:");
while (i<=10)
{
s = int.Parse(Console.ReadLine());
if (s > max)
{
max1 = max;
max = s;
}
else if (s > max1)
{
s = max1;
}
i++;
}
Console.WriteLine("最大值為{0},次大值為{1}",max,max1);
2. 編一個程式,要求使用while陳述句,輸入用戶名和密碼,實作用戶登錄程式的功能,至多允許輸入三次,超過三次不允許登錄,
const string UserName = "admin";
const string PassWord = "123456";
string username, password;
int i=1;
while(i<=3)
{
Console.WriteLine("請輸入用戶名");
username = Console.ReadLine();
Console.WriteLine("請輸入密碼");
password = Console.ReadLine();
if (username == UserName && password == PassWord)
break;
else
Console.WriteLine("你輸入的用戶名或者密碼錯誤,請重新輸入用戶名和密碼");
i++;
}
if (i <= 3)
{
Console.WriteLine("正在登錄系統");
}
else
Console.WriteLine("不能登錄系統");
29. 有關系式11+22+33+…+kk<2000,編一個程式,求出滿足此關系式的k的最大值,
int k = 1,s=0;
while (s < 2000)
{
s = s + k * k;
k++;
}
Console.WriteLine("k的值為{0}",k-2);
30. 撰寫一個程式,要求用while回圈陳述句,列印1到100的正整數,每行列印5個數,每列右對齊,
int i = 1;
while (i <= 100)
{
Console.Write("{0,10:d1}",i);
if (i % 5 == 0)
Console.WriteLine();
i++;
}
31.Fibonacci序列的前二項是0和1,且每一個后繼項是前二項的和,編一個程式,輸出項值不大于100的Fibonacci序列,
int f1 = 0;
int f2 = 1;
int f3;
while(f1<100)
{
f3=f1+f2;
Console.Write("{0},",f1);
f1=f2;
f2=f3;
}
32.(40)編一個程式,輸入a,b,c的值,求出一元二次方程axx+bx+c=0的二個實數根,計算二個實數根必須使用Math類中的Sqrt()方法,計算指定數的開方,計算二個實數根,可以用公式(-b+Math.Sqrt(bb-4ac))/(2a)和(-b-Math.Sqrt(bb-4ac))/(2*a)
int a, b, c;
double x1, x2;
Console.WriteLine("請輸入a,b,c的值");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());
if (b * b - 4 * a * c >= 0)
{
x1 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
Console.WriteLine("這個方程的兩個根是{0},{1}", x1, x2);
}
else
Console.WriteLine("這個方程沒有解");
33.編一個程式,利用while回圈陳述句,找出2到100之間的素數,
int i = 2, j;
bool b;
Console.WriteLine("2到100中的素數為:");
while (i <= 100)
{
b = true;
j = 2;
while (j <= i - 1)
{
if (i % j == 0)
{
b = false;
break;
}
j++;
}
if (b==true) Console.Write("{0} ", i);
i++;
}
Console.WriteLine();
34.(20)編一個程式,用while回圈陳述句實作下列功能:有一籃雞蛋,不止一個,有人兩個兩個數,多余一個,三個三個數,多余一個,再四個四個地數,也多余一個,請問這籃雞蛋至少有多少個
int num=2;
while(num<10000)
{
if (num % 2 == 1 && num % 3 == 1 && num % 4 == 1)
{
Console.Write("這籃雞蛋至少有{0}個", num);
break;
}
num++;
}
35.編一個程式,用for回圈陳述句求出1到100之間的奇數之和,
int i,s=0;
for (i = 1; i <= 100; i = i + 2)
{
s = s + i;
}
Console.WriteLine("1-100之間所有奇數的和為{0}",s);
36. 編一個程式,利用二重for回圈陳述句,列印出九九乘法口訣表,
int s;
for (int i = 1; i <= 9; i++)
{
for(int j=1;j<=i;j++)
{
s=i*j;
Console.Write("{0}*{1}={2} ",j,i,s);
}
Console.WriteLine();
}
37. 編一個程式,定義一個有10個元素的陣列,使用for回圈陳述句,從鍵盤上輸入10名同學的數學成績,分別求出最高分和最低分,并且求出10名同學的數學平均成績,
double [] math=new double [10];
Console.WriteLine("請輸入10名學生的數學成績");
for (int i = 0; i < math.Length; i++)
{
math[i] = double.Parse(Console.ReadLine());
}
double max=math[0], min=math[0],ave,sum=0;
for (int j = 0; j < math.Length; j++)
{
if (max < math[j])
max = math[j];
if (min > math[j])
min = math[j];
sum = sum + math[j];
}
ave = sum / math.Length;
Console.WriteLine("最高分為{0},最低分為{1},平均分為{2}",max,min,ave);
38.編一個程式,利用for回圈陳述句,求出1!+2!+3!+…+10!的和,
int s = 0, jc = 1;
for (int j = 1; j <= 10; j++)
{
jc = jc * j;
s = s + jc;
}
Console.WriteLine("1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的和為{0}", s);
39.編一個程式,用for回圈陳述句求出1到100之間的奇數之和,以及偶數之和,
long i, j, m, s = 0;
for (i = 1; i <= 10; i++)
{
m = 1;
for (j = 1; j <= i; j++) m = m * j;
s = s + m;
}
Console.WriteLine("1!+2!+3!+...+10!=" + s);
40.編一個程式,用for回圈陳述句,從鍵盤輸入10個實數,分別求出它們中的正數之和,以及負數之和
int i;
double sum1 = 0, sum2 = 0, x;
for (i = 1; i <= 10; i++)
{
Console.Write("請輸入一個實數:");
x = double.Parse(Console.ReadLine());
if (x > 0)
sum1 += x;
else
sum2 += x;
}
Console.WriteLine("正數之和:{0},負數之和:{1}", sum1, sum2);
41. 編一個程式,把一張1元的鈔票換成5分,2分和1分的硬幣,要求每種至少一枚,并且所換硬幣數不超過30枚,請問,有哪幾種換法?
int i, j, k;
Console.WriteLine("一分:\t二分:\t五分:");
for (i = 1; i <= 100; i++)
for (j = 1; j <= 50; j++)
for (k = 1; k <= 20; k++)
if (i + 2 * j + 5 * k == 100 && i + j + k <= 30)
Console.WriteLine("{0}枚\t{1}枚\t{2}枚", i, j, k);
42. 編一個程式,把一張面值100元的鈔票換成5元,1元和5角面值的鈔票,要求100元換以上的零錢100張,且要求每種不少于一張,請問,有哪幾種換法?
int i, j, k;//設5元有i張,1元有j張,5角有k張
Console.WriteLine("5元:\t1元:\t5角:");
for (i = 1; i <= 20; i++)
for (j = 1; j <= 100; j++)
for (k = 1; k <= 100; k++)
if (5 * i + j + (0.5 * k) == 100 && i + j + k == 100) Console.WriteLine("{0}\t{1}\t{2}", i, j, k);
43. 編一個程式,解決百錢買百雞問題,某人有100元錢,要買100只雞,公雞5元錢一只,母雞3元錢一只,小雞一元錢3只,問可買到公雞,母雞,小雞各為多少只,問題分析:設公雞x只,母雞y只,小雞z只,可以列出兩個方程:
x+y+z=100
5x+3y+z/3=100
我們采用“窮舉法”來解決此問題,
double z;
for( double x=1;x<=20;x++)
for (double y = 1; y <= 33; y++)
{
z = 100 - x - y;
if (5 * x + 3 * y + z / 3 == 100)
{
Console.WriteLine("公雞{0}只,母雞{1}只,小雞{2}只",x,y,z);
}
}
44.編一個程式,定義陣列,用for回圈陳述句,順序輸入10個實數,然后逆序輸出這10個數,
int i;
int[] a = new int[10];
for (i = 0; i < 10; i++)
{
Console.Write("請輸入一個數:");
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
for (i = 9; i >= 0; i--)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine();
45.編一個程式,從鍵盤輸入10個實數,存入一個陣列,用冒泡法對這個數作升序排序,
int i, j, temp;
const int n = 10;
int[] SortArray = new int[n];
for (i = 0; i < n; i++)
{
Console.Write("\n請輸入一個數:");
SortArray[i] = int.Parse(Console.ReadLine());
}
Console.Write("\n等待排序的資料序列為:");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", SortArray[i]);
}
for (i = n - 1; i >= 0; i--)
{
for (j = 0; j <= i - 1; j++)
{
if (SortArray[j] > SortArray[j + 1])
{
temp = SortArray[j];
SortArray[j] = SortArray[j + 1];
SortArray[j + 1] = temp;
}
}
}
Console.Write("\n已經排序后的資料序列為:");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", SortArray[i]);
}
Console.WriteLine();
46.編一個程式,定義一個有10個元素的一維陣列a,在鍵盤上輸入時沒有大小次序,但是存入陣列時要按由小到大的順序存放,例如,輸入第1個數1時,存入a[0];假如第2個數是5,則數存入a[1];假如第3個數是4,那么把前面輸入的5向后面移動到a[2],把4插入到a[1]的位置上,這樣使得每輸入一個數,保持從小到大的順序排列,
int i, j, temp, n = 10;
int[] a = new int[n];
Console.WriteLine("請輸入{0}個整數,", n);
for (i = 0; i < n; i++)
{
Console.Write("請輸入一個整數:");
a[i] = int.Parse(Console.ReadLine());
for (j = i; j >= 1; j--)
{
if (a[j - 1] > a[j])
{
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
else
break;
}
}
Console.Write("\n依次輸出陣列中的值:");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine();
47.編一個程式,定義一個陣列,用for陳述句輸入10個實數存入這個陣列,然后按逆序重新存放后再輸出,
double[] a = new double[10];
double temp;
for (int i = 0; i < 10; i++)
{
Console.Write("請輸入一個實數:");
a[i] = double.Parse(Console.ReadLine());
}
for (int i = 0; i < 10 / 2; i++)
{
temp = a[i];
a[i] = a[9 - i];
a[9 - i] = temp;
}
for (int i = 0; i < 10; i++) Console.Write(" {0}", a[i]);
Console.WriteLine();
48.編一個程式,從鍵盤輸入一個字串,用foreach回圈陳述句,統計其中大寫字母的個數和小寫字母的個數,
string s;
int n1=0,n2=0;
Console.WriteLine("請輸入一個字串");
s = Console.ReadLine();
foreach (char c in s)
{
if (c >= 'A' && c <= 'Z')
n1++;
else if (c >= 'a' && c <= 'z')
n2++;
else
continue;
}
Console.WriteLine("大寫字母有{0}個,小寫字母有{1}個",n1,n2);
49. 編一個程式,定義一個字符陣列和一個字串變數,給這個字串變數輸入一個字串,然后用foreach陳述句把這個字串拷貝到字符陣列里,最后輸出字符陣列,
string s;
Console.WriteLine("請輸入一個字串");
s = Console.ReadLine();
char[] ch=new char [s.Length];
int n=0;
foreach (char c in s)
{
ch[n] = c;
n++;
}
foreach (char c in ch)
{
Console.WriteLine(c);
}
50.輸入一個字串,將其中小寫字母改成大寫字母,把大寫字母改成小寫字母,其余字符不變,輸出該字串,
string s;
Console.WriteLine("請輸入一個字串:");
s = Console.ReadLine();
foreach (char ch in s)
if ('a' <= ch && ch <= 'z') Console.Write((char)((int)ch - 32));
else if ('A' <= ch && ch <= 'Z') Console.Write((char)((int)ch + 32));
else Console.Write(ch);
Console.WriteLine();
51.編一個程式,輸入一個字串,用ToCharArray()方法把字串中的內容拷貝到字符陣列中,然后用foreach輸出該字符陣列,
string s;
Console.WriteLine("請輸入一個字串");
s = Console.ReadLine();
char[] ch = new char[s.Length];
ch = s.ToCharArray();
foreach (char c in ch)
{
Console.WriteLine(c);
}
52.(20)編一個程式,定義一個字串變數,輸入字串,然后再輸入一個字符,在字串中查找該字符出現的次數,
string a;
char b;
int d = 0;
Console.WriteLine("請輸入一個字串");
a = Console.ReadLine();
Console.WriteLine("請輸入一個字符");
b = char.Parse(Console.ReadLine());
for (int c = 0; c <= a.Length - 1; c++)
{
if (a[c] == b)
d++;
}
Console.WriteLine("字符{0}在字串{1}中出現了{2}次", b, a, d);
53.編一個程式,定義一個字串變數,輸入字串,判斷有沒有連續重復字符出現,統計重復字符出現次數,例如,aaabccdfff,其中a重復出現二次,c重復出現一次,f重復出現二次,共計字符重復五次,
string i;
int a = 0;
Console.WriteLine("輸入一個字串");
i = Console.ReadLine();
for (int b = 1; b <= i.Length - 1; b++)
{
if (i[b] == i[b - 1])
a++;
}
Console.WriteLine("{0}", a);
54.編一個程式,利用for陳述句,列印一個如下形式的一個直角三角形,頂點在螢屏中線上,行數從鍵盤輸入,
*
**
***
****
int i, j, n = 0, m = 40;
while (n <= 0)
{
Console.WriteLine("請輸入一個正整數:");
n = int.Parse(Console.ReadLine());
}
Console.WriteLine('\n');
for (i = 1; i <= n; i++)
{
Console.Write('\n');
for (j = 1; j <= m; j++) Console.Write(' ');
for (j = 1; j <= i; j++) Console.Write('*');
}
Console.WriteLine();
55. 編一個程式,利用for陳述句,列印一個如下形式的一個等腰三角形,頂點在螢屏中線上,行數從鍵盤輸入,
*
***
*****
*******
int i;
Console.WriteLine("從鍵盤上輸入一個數");
i = int.Parse(Console.ReadLine());
for (int b = 1; b <= i ; b++)
{
for (int a = 1; a <= i - b; a++)
{
Console.Write(" ");
}
for (int c = 1; c <= 2 * b - 1; c++)
{
Console.Write ("*");
}
Console.WriteLine();
}
56.定義一個一維陣列,輸入任意6個整數,假定為7,4,8,9,1,5,編一個程式,利用for陳述句,輸出如下方陣的內容:
7 4 8 9 1 5
5 7 4 8 9 1
1 5 7 4 8 9
9 1 5 7 4 8
8 9 1 5 7 4
4 8 9 1 5 7
int[] a ={ 7, 4, 8, 9, 1, 5 };
int temp;
for (int i = 0; i < 6; i++)
{
Console.Write("{0} ",a[i]);
}
for(int i=0;i<5;i++)
{
Console.WriteLine();
temp = a[5];
for (int j = 5; j >0; j--)
{
a[j] = a[j - 1];
}
a[0] = temp;
for (int k = 0; k < 6; k++)
Console.Write("{0} ",a[k]);
}
57.(20)編一個程式,定義一個n行n列的二維整數陣列,賦初值,然后求出對角線上的元素之和,
int n = 5;
int s=0;
int[,] arr ={ { 1, 2, 3, 4, 5 }, { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 }, { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 } };
int i, j;
for (i = 0; i < arr.GetLength(0); i++)
{
for (j = 0; j < arr.GetLength(1); j++)
{
if (i == j || i + j == n + 1)
s = s + arr[i, j];
}
}
Console.WriteLine("對角線上的元素之和{0}",s);
58.編一個程式,輸入一個正整數,判斷它是否為素數,如不是,則輸出其所有正約數,
Console.WriteLine("請輸入一個正整數:");
uint i, n, flag;
bool f = true;
n = uint.Parse(Console.ReadLine());
//uint.TryParse(Console.ReadLine(),out n);
for (i = 2; i < n; i++)
{
if (n % i == 0) { f = false; break; }
}
if (f) Console.WriteLine("{0}是一個素數,", n);
else
{
Console.Write("{0}不是一個素數,\n{1}=", n, n);
i = 2;
flag = 0;
while (i <= n)
{
while (n % i == 0)
{
n = n / i;
if (flag != 0) Console.Write('*');
flag = 1;
Console.Write("{0}", i);
}
i++;
}
}
59.(40)編一個程式,輸入一個代表身份證號碼的字串,判斷字串長度(18)是否正確,還要判斷輸入的每個字符是否為數字,否則要求重新輸入,
string a;
bool q = true;
Console.WriteLine("請輸入你的身份證;");
a = Console.ReadLine();
for (int b=0; b <= a.Length - 1; b++)
{
if (a[b] >= '0' && a[b] <= '9')
{
continue;
}
else
q=false;
}
if (a.Length == 18)
{
if(q==true)
Console.WriteLine("輸入正確");
else if(q==false)
Console.WriteLine("輸入不合法,請重新輸入");
}
else
Console.WriteLine("輸入不合法,請重新輸入");
60.編一個程式,輸入一個正整數n,把它轉換為二進制數,并輸出,提示:應該利用陣列,
int[] a = new int[80];
int i, j, n = 0;
while (n <= 0)
{
Console.WriteLine("請輸入一個正整數:");
n = int.Parse(Console.ReadLine());
}
i = 0;
Console.Write("\n正整數{0}轉換為二進制數:", n);
while (n > 0)
{
a[++i] = n % 2;
n = n / 2;
}
for (j = i; j > 0; j--) Console.Write(a[j]);
Console.WriteLine();
61. 所謂“降序數”是指一個自然數的低位數字不大于高位數字的數,例如:64,55,321都認為是降序數,但是623不是降序數,一位數字被認為是降序數,編一個程式,輸入一個數,判定它是否為降序數,該程式是回圈執行的,當輸入的數為0時,則退出程式運行,
int i, j, m, n;
bool pos;
while (true)
{
n = -1;
while (n <= 0)
{
if (n == 0) return;
Console.WriteLine("請輸入一個正整數或者0:");
n = int.Parse(Console.ReadLine());
}
if (n < 10) pos = true;
else
{
m = n;
i = 0;
pos = true;
while (m > 0)
{
j = m % 10;
m = m / 10;
if (i > j)
{
pos = false;
break;
}
i = j;
}
}
if (pos) Console.WriteLine("{0}是降序數,", n);
else Console.WriteLine("{0}不是降序數,", n);
}
62.(40)所謂“回文數”是指讀一個自然數,從正方向讀和反方向讀,結果是一樣的,例如:646,1551,891232198都認為是回文數,編一個程式,輸入一個正整數,判定它是否為回文數,當輸入的數為0時,則退出程式,否則繼續回圈執行程式,
int s, k;
int i, j;
int[] a = new int[20];
bool pos;
while (true)
{
s = -1;
while (s < 0)
{
Console.Write("請輸入一個正整數或者只按一個數字0:");
s = int.Parse(Console.ReadLine());
if (s == 0) return;
}
k = s;
pos = true;
i = -1;
while (k > 0)
{
i++;
a[i] = k % 10;
k = k / 10;
}
//注意:陣列a的長度為(i+1)
for (j = 0; j < (i + 1) / 2; j++)
if (a[j] != a[i - j])
{
pos = false;
break;
}
if (pos) Console.WriteLine("{0}是回文數,", s);
else Console.WriteLine("{0}不是回文數,", s);
}
63.編一個程式,定義一個n行n列的二維陣列,例如,n=4,輸入該陣列的全部資料,可以在定義陣列時賦于常量值,求二維陣列中這樣元素的位置:它在行上是最小,在列上也是最小,(注意:它未必是整個陣列的最小元素,)
int n = 4;
int[,] a ={ { 25, 12, 22, 14 }, { 15, 16, 17, 18 }, { 19, 20, 21, 13 }, { 23, 24, 11, 26 } };
int i, j, k, temp, min;
Console.Write("\n二維陣列:");
for (i = 0; i < n; i++)
{
Console.Write("\n\t");
for (j = 0; j < n; j++) Console.Write("{0} ", a[i, j]);
}
Console.WriteLine("\n\n 行 列 最小值");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
temp = a[i, j];
min = temp;
/*
for(k=0;k<n;k++)
{
if(a[i,k]<min)min=a[i,k];
if(a[k,j]<min)min=a[k,j];
}
*/
for (k = 0; k < n; k++)
if (a[i, k] < min) min = a[i, k];//在第i行查找最小值
for (k = 0; k < n; k++)
if (a[k, j] < min) min = a[k, j];//在第j列查找最小值
if (temp == min) Console.WriteLine("i={0},j={1} {2}", i, j, temp);
}
64.給定一個m×n的數值矩陣A,如果矩陣A中存在這樣的一個元素A[i][j]滿足條件:A[i][j]是第i行中值最小的元素,且又是第j列中值最大的元素,則稱之為該矩陣的一個馬鞍點,撰寫一個方法計算出m*n的矩陣A的所有馬鞍點,
int i, j, k, m = 4, n = 5, max, min;
int[,] array = new int[,] {
{ 30, 20, 25, 40, 45 },
{ 80, 19, 70, 90, 11 },
{ 24, 14, 42, 91, 96 },
{ 32, 17, 82, 72, 38 }
};
Console.Write("\n{0}行{1}列的數值矩陣為:");
for (i = 0; i < m; i++)
{
Console.WriteLine();
for (j = 0; j < n; j++)
{
Console.Write("{0} ", array[i, j]);
}
}
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
max = array[i, j];
min = array[i, j];
for (k = 0; k < n; k++) if (array[i, k] < min) min = array[i, k];
for (k = 0; k < m; k++) if (array[k, j] > max) max = array[k, j];
if (array[i, j] == max && array[i, j] == min)
{
Console.WriteLine("\n馬鞍點是第{0}行,第{1}列的{2}", i, j, array[i, j]);
}
}
}
65. 編一個程式,輸入一個整數,判定它為幾位數,例如,99是2位數,-100是3位數,
int m, n, k = 0;
Console.Write("請輸入一個整數:");
m = int.Parse(Console.ReadLine());
n = Math.Abs(m);
while (n != 0)
{
n = n / 10;
k++;
}
Console.WriteLine("{0}是{1}位數,", m, k);
66.編一個程式,定義一個字串陣列,輸入若干國家名稱(所有名稱全用大寫或者全用小寫),設計一個演算法按字典順序將這些國家名稱進行排序,
int i, j, n = 10;
string[] SortArray = new string[n];
string temp;
Console.Write("\n請輸入{0}個字串:", n);
for (i = 0; i < n; i++)
{
SortArray[i] = Console.ReadLine();
}
for (i = 1; i < n; i++)
{
for (j = 0; j < n - i; j++)
{
if (String.Compare(SortArray[j], SortArray[j + 1]) > 0)
{
temp = SortArray[j];
SortArray[j] = SortArray[j + 1];
SortArray[j + 1] = temp;
}
}
}
Console.WriteLine("\n已經排序后的國家名稱為:");
for (i = 0; i < n; i++)
{
Console.WriteLine(SortArray[i]);
}
67. 編一個程式,定義類student和它的成員(學號,姓名,年齡和c_sharp程式設計成績),用類student生成物件s,分別對物件s的成員賦值,然后輸出物件s,
class student
{
public string 學號;
public string 姓名;
public int 年齡;
public int c_sharp程式設計成績;
}
static void Main(string[] args)
{
student s;
s = new student();
s.學號 = "09061234";
s.姓名 = "劉云飛";
s.年齡 = 21;
s.c_sharp程式設計成績 = 95;
Console.WriteLine("學生學號:{0}", s.學號);
Console.WriteLine("學生姓名:{0}", s.姓名);
Console.WriteLine("學生年齡:{0}", s.年齡);
Console.WriteLine("c_sharp程式設計成績:{0}", s.c_sharp程式設計成績);
}
68. 編一個程式,定義結構(有姓名,年齡,手機號碼三個欄位),再定義一個一維陣列,把結構作為陣列元素型別,存入資料,然后依次輸出,
struct Contact
{
public string name;
public int age;
public string telephone;
}
class Program
{
static void Main(string[] args)
{
Contact[] c = new Contact[3];
//c[0]=new Contact();//此三個陳述句,對于結構型別可以不用
//c[1]=new Contact();
//c[2]=new Contact();
c[0].name="劉龍";
c[0].age=54;
c[0].telephone="13970861234";
c[1].name="王鳴";
c[1].age=42;
c[1].telephone="13813254321";
c[2].name="張星";
c[2].age=39;
c[2].telephone="13751461818";
Console.Write("姓名:{0},",c[0].name);
Console.Write("年齡:{0},",c[0].age);
Console.WriteLine("手機號碼:{0}",c[0].telephone);
Console.Write("姓名:{0},",c[1].name);
Console.Write("年齡:{0},",c[1].age);
Console.WriteLine("手機號碼:{0}",c[1].telephone);
Console.Write("姓名:{0},",c[2].name);
Console.Write("年齡:{0},",c[2].age);
Console.WriteLine("手機號碼:{0}",c[2].telephone);
}
}
69. 編一個程式,定義類(有姓名,年齡,手機號碼三個欄位),再定義一個一維陣列,使陣列元素為類,存入資料,然后依次輸出,使用for回圈陳述句進行輸入輸出操作,
class Contact
{
public string name;
public int age;
public string telephone;
}
class Program
{
static void Main(string[] args)
{
int i, n = 3;
Contact[] c = new Contact[n];
for (i = 0; i < n; i++) c[i] = new Contact();
for (i = 0; i < n; i++)
{
Console.Write("請輸入學生姓名:");
c[i].name = Console.ReadLine();
Console.Write("請輸入學生年齡:");
c[i].age = int.Parse(Console.ReadLine());
Console.Write("請輸入手機號碼:");
c[i].telephone = Console.ReadLine();
}
for (i = 0; i < n; i++)
{
Console.Write("姓名:{0},", c[i].name);
Console.Write("年齡:{0},", c[i].age);
Console.WriteLine("手機號碼:{0}", c[i].telephone);
}
}
}
70. 編一個程式,輸入三個double型別資料,自定義一個靜態方法,把這三個數送給它,回傳找出的最大數,
class classname
{
public static double compare(double x, double y, double z)
{
double temp;
if (x > y) temp = x;
else temp = y;
if (z > temp) temp = z;
return temp;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("請輸入一個實數:");
double x = double.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
double y = double.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
double z = double.Parse(Console.ReadLine());
double temp = classname.compare(x, y, z);
Console.WriteLine("比較后得到的最大數為{0}", temp);
}
}
71.編一個程式,利用自定義方法,找出2到100之間的素數,
class primetest
{
public bool prime(int x)
{
for (int i = 2; i < x; i++)
{
if (x % i == 0) return false;
}
return true;
}
}
class Program
{
static void Main(string[] args)
{
primetest c = new primetest();
Console.WriteLine("2到100中的素數:");
for (int i = 2; i < 100; i++)
if (c.prime(i))
Console.Write("{0} ", i);
}
}
72.編一個程式,從鍵盤上輸入三個double型別的數,自定義方法,以從小到大的順序排序,以參考型引數呼叫方法,然后回傳主方法輸出結果,
class compare
{
public void swap(ref double x, ref double y, ref double z)
{
double temp;
if (x > y)
{
temp = x;
x = y;
y = temp;
double temp;
}
if (y > z)
{
temp = x;
y = z;
z = temp;
double temp;
}
if (x > y)
{
temp = x;
x = y;
y = temp;
}
}
}
class Program
{
static void Main(string[] args)
{
double x, y, z;
compare c = new compare();
Console.Write("請輸入一個實數:");
x = double.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
y = double.Parse(Console.ReadLine());
Console.Write("請輸入一個實數:");
z = double.Parse(Console.ReadLine());
Console.WriteLine("排序前的三個數依次為{0},{1},{2}", x, y, z);
c.swap(ref x, ref y, ref z);
//c.swap(ref x,ref z);
//c.swap(ref y,ref z);
Console.WriteLine("排序后的三個數依次為{0},{1},{2}", x, y, z);
}
}
73. 編一個程式,輸入a,b,c的值,定義一個靜態方法,求出一元二次方程axx+bx+c=0的二個實數根,計算二個實數根必須使用Math類中的Sqrt()方法,計算指定數的開方,計算二個實數根,可以用公式(-b+Math.Sqrt(bb-4ac))/(2a)和(-b-Math.Sqrt(bb-4ac))/(2*a),
class ClassName
{
public static void root(double a, double b, double c, ref double root1, ref double root2)
{
root1 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
root2 = (b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
}
}
class Program
{
static void Main(string[] args)
{
double a, b, c, root1 = 0, root2 = 0;
Console.Write("請輸入一個數:");
a = double.Parse(Console.ReadLine());
Console.Write("請輸入一個數:");
b = double.Parse(Console.ReadLine());
Console.Write("請輸入一個數:");
c = double.Parse(Console.ReadLine());
if (a != 0 && b * b - 4 * a * c >= 0)
{
ClassName.root(a, b, c, ref root1, ref root2);
Console.WriteLine("root1={0},root2{1}", root1, root2);
}
else Console.WriteLine("輸入的資料有錯誤");
}
}
74. 編一個程式,定義一個陣列,再定義類以及方法,用方法(out引數傳遞資料)找出這組資料中的最大數和最小數,
class ClassName
{
public void find(out int max, out int min, params int[] array)
{
int n = array.Length, i = 1;
max = array[0];
min = array[0];
while (i < n)
{
if (array[i] > max)
max = array[i];
else
if (array[i] < min) min = array[i];
i++;
}
}
}
class Program
{
static void Main(string[] args)
{
int max, min;
int[] a = new int[10] { 97, 65, 32, 17, 58, 43, 79, 81, 23, 10 };
ClassName s = new ClassName();
s.find(out max, out min, a);
Console.WriteLine("最大數為:" + max);
Console.WriteLine("最小數為:" + min);
}
}
75. 編一個程式,定義一個類,該類中定義二個非靜態方法,一個方法用來求出這個三角形的周長,另一個方法用來求出這個三角形的面積,已知三角形三條邊a,b,c,計算其面積可以用Math類中的Sqrt()方法,有運算式Math.Sqrt(s(s-a)(s-b)(s-c)),可以利用它計算指定數的開方,其中s=(a+b+c)/2,在主方法中輸入一個三角形三條邊a,b,c,要求呼叫這二個非靜態方法計算三角形的周長和面積,注意:在輸入三角形三條邊時,必須檢查它們的資料合法性,*
class triangle
{
public double c(double a, double b, double c)
{
double zc;
zc = a + b + c;
return zc;
}
public double s(double a, double b, double c)
{
double mj,s1;
s1 = (a + b + c) / 2;
mj = Math.Sqrt(s1*(s1-a)*(s1-b)*(s1-c));
return mj;
}
}
class Program
{
static void Main(string[] args)
{
triangle tr=new triangle ();
double x, y, z;
Console.WriteLine("請輸入三角形的三邊");
x = double.Parse(Console.ReadLine());
y = double.Parse(Console.ReadLine());
z = double.Parse(Console.ReadLine());
if ((x + y > z && x - y < z) && (x + z > y && x - z < y) && ((y + z) > x && (y - z < x)))
{
Console.WriteLine("三角形的周長為{0}", tr.c(x, y, z));
Console.WriteLine("三角形的面積為{0}", tr.s(x, y, z));
}
else
Console.WriteLine("三角形三邊不合法");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/276228.html
標籤:其他
上一篇:VLAN隔離
