题目一:分别控制台SDK方式及集成开发IDE方式编写C#程序,显示输出字符串“这是我的第一个C#程序。”。
源程序:
SDK方式(记事本输入)
新建一个txt文本文件
using System;
namespace Test{
class Hello{
static void Main(string[] args){
Console.WriteLine("这是我的第一个C#程序。");
Console.ReadKey();
}
}
}
命令行输入: cd / (切换至新建txt文件对应的目录)
csc test.cs
test
IDE方式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("这是我的第一个C#程序。");
Console.ReadKey();
}
}
}
题目二:分别按int,long,float,double类型定义两个变量并赋初值,并计算同类型变量的和、差、积、商,最后输出相应的运算结果。
源程序:
int型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
int a = 30;
int b = 20;
Console.WriteLine("两数之和:"+(a + b));
Console.WriteLine("两数之差:" + (a - b));
Console.WriteLine("两数之积:" + (a * b));
Console.WriteLine("两数之商:" + (a / b));
Console.ReadLine();
}
}
}
long型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
long a = (long)300;
long b = (long)200;
Console.WriteLine("两数之和:"+(a + b));
Console.WriteLine("两数之差:" + (a - b));
Console.WriteLine("两数之积:" + (a * b));
Console.WriteLine("两数之商:" + (a / b));
Console.ReadLine();
}
}
}
float型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
float a = (float)15.5;
float b = (float)32.9;
Console.WriteLine("两数之和:"+(a + b));
Console.WriteLine("两数之差:" + (a - b));
Console.WriteLine("两数之积:" + (a * b));
Console.WriteLine("两数之商:" + (a / b));
Console.ReadLine();
}
}
}
double型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
double a = (double)54.8;
double b = (double)11.9;
Console.WriteLine("两数之和:"+(a + b));
Console.WriteLine("两数之差:" + (a - b));
Console.WriteLine("两数之积:" + (a * b));
Console.WriteLine("两数之商:" + (a / b));
Console.ReadLine();
}
}
}
题目三:试编程,从键盘任意输入两个整数分别赋值给a和b,并比较两个数的大小,按从小到大的顺序输出两个数。
源程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
int a,b,temp;
Console.WriteLine("请输入第一个数:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第一个数:");
b = int.Parse(Console.ReadLine());
if (a >b)
{
temp = a;
b= temp;
a = b;
}
Console.WriteLine("结果为:"+a+"<"+b);
Console.ReadKey();
}
}
}
源程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyExer1
{
internal class Program
{
static void Main(string[] args)
{
int x,y = 0;
Console.WriteLine("请输入一个整数:");
x=int.Parse(Console.ReadLine());
if (x >= 1)
{
y = y + 2 * x + 1;
}
else {
y = y + 3 * x / (x - 1);
}
Console.WriteLine("结果为:" + y);
Console.ReadKey();
}
}
}