zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#实验报告上机一

c# 上机 实验报告
2023-09-14 09:14:09 时间

1.分别控制台SDK方式及集成开发IDE方式编写C#程序,显示输出字符串“这是我的第一个C#程序。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 1.分别控制台SDK方式及集成开发IDE方式编写C#程序,显示输出字符串“这是我的第一个C#程序。
namespace Suke.day02
{
    internal class Demo1
    {
        static void Main(string[] args)
        {
            Console.WriteLine("这是我的第一个C#程序。");
            Console.ReadKey();
        }
    }
}

 

 

2.分别按int,long,float,double类型定义两个变量并赋初值,并计算同类型变量的和、差、积、商,最后输出相应的运算结果。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 2.分别按int,long,float,double类型定义两个变量并赋初值,并计算同类型变量的和、差、积、商,最后输出相应的运算结果。
namespace Suke.day02
{
    internal class Demo2
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            Console.WriteLine("a + b = {0}", a + b);
            Console.WriteLine("a - b = {0}", a - b);
            Console.WriteLine("a * b = {0}", a * b);
            Console.WriteLine("a / b = {0}", a / b);
            Console.WriteLine("a % b = {0}", a % b);
            
            long c = 3;
            long d = 4;
            Console.WriteLine("c + d = {0}", c + d);
            Console.WriteLine("c - d = {0}", c - d);
            Console.WriteLine("c * d = {0}", a * b);
            Console.WriteLine("c / d = {0}", c / d);
            Console.WriteLine("c % d = {0}", c % d);
                
            float f = 5;
            float g = 6;
            Console.WriteLine("f + g = {0}", f + g);
            Console.WriteLine("f - g = {0}", f - g);
            Console.WriteLine("f * g = {0}", f * g);
            Console.WriteLine("f / g = {0}", f / g);  
            Console.WriteLine("f % g = {0}", f % g);
                
            double h = 7;
            double i = 8;
            Console.WriteLine("h + i = {0}", h + i);
            Console.WriteLine("h - i = {0}", h - i);
            Console.WriteLine("h * i = {0}", h * i);
            Console.WriteLine("h / i = {0}", h / i);
            Console.WriteLine("h % i = {0}", h % i);
            Console.ReadKey();
        }
    }
}

 

3.试编程,从键盘任意输入两个整数分别赋值给a和b,并比较两个数的大小,按从小到大的顺序输出两个数。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//3.试编程,从键盘任意输入两个整数分别赋值给a和b,并比较两个数的大小,按从小到大的顺序输出两个数。
namespace Suke.day02
{
    internal class Demo3
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入两个整数分别赋值给a和b");
            String s1 = Console.ReadLine();
            String s2 = Console.ReadLine();
            int a = int.Parse(s1);
            int b = int.Parse(s2);
            if(a <= b)
            {
                Console.WriteLine("a = {0}", a);
                Console.WriteLine("b = {0}", b);
            }else if(a > b)
            {
                Console.WriteLine("b = {0}", b);
                Console.WriteLine("a = {0}", a);
            }
            Console.ReadKey();
        }
    }
}

 

 

4.试编程,从键盘任意输入一个整数给变量x,计算并输出分段函数y的值: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//4.试编程,从键盘任意输入一个整数给变量x,计算并输出分段函数y的值:
namespace Suke.day02
{
    internal class Demo4
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入一个整数给变量x");
            String s = Console.ReadLine();
            int x = Convert.ToInt32(s);
            float y;
            if (x >= 1)
            {
                y = 2 * x + 1;
                Console.WriteLine(y);
            }else if (x < 1)
            {
                y = (3 * x) / (x - 1);
                Console.WriteLine(y);
            }
            Console.ReadKey();
        }
    }
}