刚学习c#,想输入整数,却突然发现不知道该怎么输入,网上查了下怎么输入
下面是代码可以用Convert、Parse、TryParse、(int)
1.使用convert
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace aaa //lx424
{
class arraytest
{
static void ain()
{
int[] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
arr[i] = Convert.ToInt32( Console.ReadLine() );
for (int i = 0; i < arr.Length; i++)
Console.WriteLine("{0}", arr[i]);
}
}
}
2.使用parse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class expretest
{
public static void Main()
{
Console.WriteLine("enter a number:");
string s = Console.ReadLine();
int mynumber = Int32.Parse(s);
Console.WriteLine("{0}", mynumber);
Console.ReadLine();
}
}