How to read an integer using console.readline()?

前端 未结 4 1463
既然无缘
既然无缘 2020-12-22 13:50

I\'m a beginner who is learning .NET.

I tried parsing my integer in console readline but it shows a format exception.

My code:

using System;         


        
4条回答
  •  悲哀的现实
    2020-12-22 14:38

    You can convert numeric input string to integer (your code is correct):

    int age = Convert.ToInt32(Console.ReadLine());
    

    If you would handle text input try this:

    int.TryParse(Console.ReadLine(), out var age);
    

提交回复
热议问题