第一范文网 - 专业文章范例文档资料分享平台

程序设计基础练习题(全答案版)

来源:用户分享 时间:2025/5/16 15:46:11 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

} }

2、有一函数,当x<1 ,y=x;当 x>=1,y=x*x,写一程序输入x,输出y。 class Program {

static void Main(string[] args) {

double x, y;

Console.Write(\请输入数值x:\);

x = Convert.ToInt16(Console.ReadLine()); if (x < 1) y = x; else

y = x * x;

Console.WriteLine(\输出y等于:{0}\,y); } } }

3、有一函数:

1 (x<0) y= 0 (x=0) -1 (x>0)

要求:输入一个x值,输出y值。用控制台应用程序编写。 class Program {

static void Main(string[] args) {

double x, y;

Console.Write(\请输入数值x:\);

x = Convert.ToInt16(Console.ReadLine()); if (x < 0) y = 1; else if (x > 0) y = -1; else

y = 0;

Console.WriteLine(\输出y等于:{0}\,y); } }

4、从键盘上任意输入两个整数,并将较大的数显示出来。 class Program {

static void Main(string[] args) {

int x, y,z;

6

Console.Write(\请输入数值x:\);

x = Convert.ToInt16(Console.ReadLine()); Console.Write(\请输入数值y:\);

y = Convert.ToInt16(Console.ReadLine()); if (x > y) z=x; else z=y;

Console.WriteLine(\输出较大的数:{0}\,z); } } 方法二: using System; class cl {

static void Main() {

Console.WriteLine(\班级:电子商务2班 , 序号:25号 ,莫子良\\n \); int x, y,z;

Console.Write(\请输入x的值:\);

x = Convert.ToInt16(Console.ReadLine()); Console.Write(\请输入y的值:\);

y = Convert.ToInt16(Console.ReadLine()); z=Math.Max(x,y);

Console.WriteLine(\输出较大值:{0}\, z); } }

5、从键盘上任意输入三个整数,并将较大的数显示出来。

using System; class jdz {

static void Main() {

double x, y, z, h,t;

Console.Write(\请输入数值一x:\

x = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值二y:\

y = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值三z:\

z = Convert.ToSingle(Console.ReadLine()); if (x > y) h = x; else

姓名:7

h = y; if (h > z) t = h; else t = z;

Console.WriteLine(\其中最大值为:{0}\ Console.ReadLine(); } }

方法二:

using System; class jdz {

static void Main() {

double x, y, z, h, t;

Console.Write(\请输入数值一x:\);

x = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值二y:\);

y = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值三z:\);

z = Convert.ToSingle(Console.ReadLine()); h = Math.Max(x, y); t = Math.Max(h, z);

Console.WriteLine(\其中最大值为:{0}\, t); Console.ReadLine(); } }

6、输入两个实数,按代数值由小到大的次序输出这两个数。using System;

namespace ConsoleApplication2 {

class Program {

static void Main(string[] args) {

int a, b, c;

Console.Write(\请输入第一个实数:\); a = Convert.ToInt16(Console.ReadLine()); Console.Write(\请输入第二个实数:\); b = Convert.ToInt16(Console.ReadLine()); if (b > a) {

c = a; a = b; b = c; }

Console.WriteLine(\由小到大排序:{0},{1}\, b,a); }

8

} }

方法二: using System;

namespace ConsoleApplication2 {

class Program {

static void Main(string[] args) {

int a, b, c, d;

Console.Write(\请输入第一个实数:\); a = Convert.ToInt16(Console.ReadLine()); Console.Write(\请输入第二个实数:\); b = Convert.ToInt16(Console.ReadLine()); if (b > a)

Console.WriteLine(\由小到大排序:{0},{1}\,a,b); else

Console.WriteLine(\由小到大排序: {0},{1}\,b,a); } } }

7、输入三个实数,按代数值由小到大的次序输出这三个数。 using System; class jdz {

static void Main() {

double a, b, c,t;

Console.Write(\请输入数值一a:\);

a = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值二b:\);

b = Convert.ToSingle(Console.ReadLine()); Console.Write(\请输入数值三c:\);

c = Convert.ToSingle(Console.ReadLine()); if(a>b)

{t=a;a=b;b=t;} if(a>c)

{t=a;a=c;c=t;} if(b>c)

{t=b;b=c;c=t;}

Console.WriteLine(\排序由小到大为:{0},{1},{2}\,a,b,c); } }

8、计算n!的程序。 using System;

9

namespace ConsoleApplication3 {

class Program {

static void Main(string[] args) {

int i; long sum = 1,n;

Console.Write(\请输入数值n:\);

n = Convert.ToInt64(Console.ReadLine()); for (i = 1; i <= n; i++) sum = sum * i;

Console.WriteLine(\的阶乘:{0}\, sum); } } }

9、求100以内的偶数和,即:2+4+6+…+100的和。 using System;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

int i, sum = 0;

for (i = 1; i <= 100; i++) {

if (i % 2 == 1) continue; sum = sum + i; }

Console.WriteLine(\, sum); } } }

10、编写一个程序,将10 , 20 , 30, 40, 50, 60 这六个数放入一个一维数组中,并输出这六个数中的最大数及最大数的位置及平均值。要求用控制台应用程序编写。

using System;

namespace ConsoleApplication1 {

class Program {

static void Main(string[] args) {

int i, max, max_i, p=0;

int[] a = { 10, 20, 30, 40, 50, 60 };

10

搜索更多关于: 程序设计基础练习题(全答案版) 的文档
程序设计基础练习题(全答案版).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1dhi24ios32r4yj9c23s_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top