【C#】上机实验一

1、开发一个控制台应用程序,根据提示从键盘获取一个华氏温度,请转换并输出对应的摄氏温度。

 1 using System;
 2 
 3 namespace Project
 4 {
 5     class Program
 6     {
 7         public static void Main(string[] args)
 8         {
 9             
10             // 提示输入 Input
11             Console.WriteLine("请输入华氏温度");   //给分点
12 
13             // 获取数据
14             string f = Console.ReadLine();
15 
16             // 类型转化: string -> double 
17             double F = Convert.ToDouble(f); //方法1 : 数据转换
18             double F_ = double.Parse(f);    //方法2 : 类型解析,把基本类型看成类
19 
20 
21             if (true)
22             {
23                 Console.WriteLine("请输入>0的华氏温度");
24             }
25             else
26             {
27                 // 公式运算
28                 double c = 5 * (F - 32) / 9;
29 
30                 // 输出 : output
31                 // 中文就是"构造字符串"
32                 Console.WriteLine("华氏温度为{0:f2},摄氏温度为:{1:f2}", c, F);
33             }
34             
35         }
36     }
37 }
华氏转摄氏

2、编写一个程序,从键盘输入一个x值,程序输出y的值。

   {  -1 + 2 * x   ,  x < 0 

y =  { -1       ,  x = 0 

   {  -1 + 3 * x  ,  x > 0 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject2
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             // Input
13             Console.WriteLine("请输入一个x值");
14             string str_x = Console.ReadLine();
15 
16             // change string -> double 
17             double x = Convert.ToDouble(str_x);
18             double y;
19 
20             //  compute
21             if (x < 0)
22             {
23                 Console.WriteLine("x < 0 : y = -1 + 2x ");
24                 y = -1 + 2 * x;
25             }
26             else if (x == 0.0)
27             {
28                 Console.WriteLine("x = 0 : y = -1 ");
29                 y = -1;
30             }
31             else
32             {
33                 Console.WriteLine("x > 0 : y = -1 + 3x");
34                 y = -1 + 3 * x;
35             }
36 
37             //output
38             Console.WriteLine("x  = {0:f2} , y = {1:f2}", x, y);
39         }
40     }
41 }
分段函数

3、在控制台中打印如下矩阵。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject3
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             for (int i = 1; i <= 16; i++)
13             {
14                 Console.Write("{0,6}",i);
15                 if (i % 4 == 0)
16                 {
17                     Console.WriteLine();
18                 }
19             }
20         }
21     }
22 }
打印矩阵

4、开发一个控制台应用程序,根据提示从键盘输入一个字符,判断此字符是数字、大写字母、小写字母还是其它字符。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject4
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string str = Console.ReadLine();
13             char ch = Convert.ToChar(str);
14             if ( '0' <= ch && ch <= '9' )
15             {
16                 Console.WriteLine("输入的为数字: {0}",ch);
17             }
18             else if ('a' <= ch && ch <= 'z')
19             {
20                 Console.WriteLine("输入的为小写字母: {0}", ch);
21             }
22             else if ('A' <= ch && ch <= 'Z')
23             {
24                 Console.WriteLine("输入的为大写字母: {0}", ch);
25             }
26             else
27             {
28                 Console.WriteLine("输入的为其他字符: {0}", ch);
29             }
30         }
31     }
32 }
判断字符类型
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             
13             Console.WriteLine("请输入一个字符");
14             int x = Console.Read();
15             char ch = (char)x;
16             string res = "";
17             if ( char.IsDigit(ch) )
18             {
19                 res = "数字";
20             }
21             else if ( char.IsLower(ch) )
22             {
23                 res = "小写字母";
24             }
25             else if ( char.IsUpper(ch) )
26             {
27                 res = "大写字母";
28             }
29             else
30             {
31                 res = "其他字符";
32             }
33             Console.WriteLine("{0} 是 {1}",ch,res);
34         }
35     }
36 }
利用静态方法判断
原文地址:https://www.cnblogs.com/Osea/p/11558197.html