1. int num;
    2. num = Convert.ToInt32(Console.ReadLine());

    Convert.ToInt32() 把用户输入的数据转换为 int 数据类型,因为 Console.ReadLine() 只接受字符串格式的数据。

    1. // See https://aka.ms/new-console-template for more information
    2. using System;
    3. namespace HelloWorld
    4. {
    5. class Program
    6. {
    7. static void Main()
    8. {
    9. string characterName = "yulin";
    10. int age = 20;
    11. Console.WriteLine("There was a person named " + characterName);
    12. Console.WriteLine("Hello, World!");
    13. Console.WriteLine("age is "+ age);
    14. int num = Convert.ToInt16(Console.ReadLine());
    15. Console.WriteLine("num is " + num);
    16. }
    17. }
    18. }