C#编程习题

更新时间:2023-09-20 02:48:01 阅读量: 小学教育 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C#编程习题

1、 编写一个函数,函数名为Max,该函数用来获取给定三个整数的最大值。在Main函数中

实现从屏幕中读取三个整数,然后输出这三个整数中最大的那个数的功能。

namespace _1 {

classProgram {

staticvoid Main(string[] args) {

Console.WriteLine(\请输入3个数:按回车结束\); int a, b, c, temp;

a = int.Parse(Console.ReadLine());//定义一个输入值a; b = int.Parse(Console.ReadLine());//定义一个输入值b; c = int.Parse(Console.ReadLine());//定义一个输入值c; temp = Max(a, b, c);

Console.WriteLine(\三个数中最大值为{3}\, a, b, c, temp); } //074 陈振华

staticint Max(int a, int b, int c) {

int temp = a;//把a的值付给temp; if (temp < b) {

temp = b; } if (temp < c) {

temp = c; } return temp; } } }

2、 编写一个函数,函数名为PrintTriangle,用来打印n阶的正三角形。并在Main函数中

实现从屏幕中输入n的值,然后根据给定的n的值打印出相应的正三角形。当n=7时, * *** ***** *******

********* *********** ************* 当n=5时, * *** ***** ******* *********

代码:

namespace _1 {

classProgram {

staticvoid Main(string[] args) { int a;

Console.WriteLine(\请输入你想要打印出的三角形的行数\); a = int.Parse(Console.ReadLine()); PrintTriangle(a); } //074 陈振华

staticvoid PrintTriangle(int a) {

for (int i = 1; i <=a; i++) {

for (int j = i; j

for (int x = i * 2 - 1; x > 0; x--) { Console.Write(\); } Console.WriteLine(\); } } } }

3、 输入某年某月某日,判断这一天是这一年的第几天?。要求:需写一个函数,给定年月

日,求的该天处于该年的第几天。然后在Main函数中测试。

代码:

namespace _1 {

classProgram {

staticvoid Main(string[] args) {

int year, month, day;

Console.WriteLine(\请输入年份\);

year = int.Parse(Console.ReadLine()); Console.WriteLine(\请输入月份\);

month = int.Parse(Console.ReadLine()); Console.WriteLine(\请输入日期\);

day = int.Parse(Console.ReadLine());

Console.WriteLine(\年{1}月{2}日处于该年的第{3}天\, year, month, day, Cs(year, month, day)); }

staticint Cs(int year, int month, int day) { int sum = 0;

for (int i = 1; i < month; i++) {

if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {

sum += 31; }

if (i == 4 || i == 6 || i == 9 || i == 11) {

sum += 30; } if (i == 2)

{

sum += 29; } }

if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month > 2) sum = sum - 1; return sum; } } }

4、 给定一个大于三的奇数n,打印出相应的三角形。

如n=7时的形状如下: * *** ***** ******* ***** *** *

代码;

classProgram {

staticvoid Main(string[] args) { int a;

Console.WriteLine(\请输入你想要打印出的三角形的行数,该数必须是大于三的奇数\); a = int.Parse(Console.ReadLine()); if (a % 2 == 1 && a >= 3) {

PrintTriangle(a); } else

Console.WriteLine(\该数不是大于三的奇数\);

} ///

///074 陈振华 ///

///

for (int i = 1; i <= a / 2 + 1; i++) {

Console.Write(\);

for (int x = 1; x < i; x++) { Console.Write(\); }

Console.WriteLine(\); }

for (int i = a / 2; i > 0; i--) {

Console.Write(\);

for (int x = i - 1; x > 0; x--) { Console.Write(\); }

Console.WriteLine(\); }

} } }

要求:编写一个函数,给定一个n,打印出相应的三角形,若n不是奇数,则提示说n的值必须为奇数。

5、 给一个正整数,要求:

a) 写一个函数求出它是几位数。

b) 写一个函数,分别输出每一位数字。

c) 写一个函数,按逆序输出各位数字,例如原数位123,则输出321. 最后在main函数中测试结果

代码:

namespace ConsoleApplication1 {

classProgram {

staticvoid Main(string[] args) { int a, r;

Console.WriteLine(\请输入一个正整数\);

a = int.Parse(Console.ReadLine()); r = C105(a);

Console.WriteLine(\该数是{0}位数\, r); int[] A = Db(a);

Shuzhu(A); Cs(A);

}

staticint C105(int a) { int i = 1; while (a > 10) {

a = a / 10; i++; } return i;

}

staticint[] Db(int a) {

int count = C105(a);

int[] data = newint[count]; while (a > 0) {

data[--count] = a % 10; a = a / 10; } return data;

}

staticvoid Shuzhu(int[] A) {

for (int i = 0; i < A.Length; i++) {

Console.Write(\,A[i]); }

Console.WriteLine(); }

staticvoid Cs(int[] A) {

for (int i = 0; i < A.Length / 2; i++) { int temp = A[i];

A[i] = A[A.Length - i - 1]; A[A.Length - i - 1] = temp; }

Console.WriteLine(\倒叙输出后的结果为\); for (int i = 0; i < A.Length; i++) {

Console.Write(\, A[i]); }

本文来源:https://www.bwwdw.com/article/ssgh.html

Top