} }
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
程序设计基础练习题(全答案版)



