Unity3d_C#接受用户输入的字符串

接受用户输入字符串以及将字符串变成整数型和浮点型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009
{
class Program
{
static void Main(string[] args)
{
//读取用户的输入
//string str= Console.ReadLine(); //人键盘上读取输入的字符串
//Console.WriteLine(str);

//Convert转型
//string str = "123";
//int num=Convert.ToInt32(str);//Convert转型类里面的有个方法可以使整数的字符串转入为整数
//num++;
//Console.WriteLine(num);//结果是124

//string str = Console.ReadLine();
//int num = Convert.ToInt32(str);
//Console.WriteLine(num);

string str = Console.ReadLine();
double num = Convert.ToDouble(str);//将字符串转化为浮点
Console.WriteLine(num);

Console.ReadKey();
}
}
}

接受用户从控制台输入两个数字,并计算和,输出到控制台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字");
string str1 = Console.ReadLine();
int num1 = Convert.ToInt32(str1);
Console.WriteLine("请输入第二个数字");
string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
Console.WriteLine("两个数字的和为");
int Sum = num1 + num2;
Console.WriteLine(Sum);

Console.ReadKey();
}
}
}