C# 无法将字符串隐式转换为int
我想做一个只做乘法运算的计算器 我正在努力C# 无法将字符串隐式转换为int,c#,C#,我想做一个只做乘法运算的计算器 我正在努力 int a; a = Console.ReadLine(); 然后它告诉我不能隐式地将字符串转换为int 我希望它读取我的int变量并将其与另一个int变量相乘,但它不允许我这样做。 多谢各位 namespace ConsoleApp9 { class Program { static void Main(string[] args) { int a; i
int a;
a = Console.ReadLine();
然后它告诉我不能隐式地将字符串转换为int
我希望它读取我的int变量并将其与另一个int变量相乘,但它不允许我这样做。
多谢各位
namespace ConsoleApp9
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
Console.WriteLine("Hey I'm a calculator in training and I'd like to test out my skills with you.");
Console.WriteLine("I can only do one type of equation right now but I'm still learning");
Console.WriteLine("What will your first number be?");
a = Console.ReadLine();
Console.WriteLine("So youre first number is ");
Console.Write(a);
Console.WriteLine(" Alrighty then what is your second number ?");
b = Console.ReadLine();
Console.WriteLine(a);
Console.WriteLine("*");
Console.WriteLine(b);
Console.WriteLine("=");
Console.WriteLine(a * b);
}
}
}
方法
Console.ReadLine;
实际上返回字符串。而int类型的变量只存储整数,而不存储整数的字符串表示形式。因此,您需要将输入从字符串转换为int。因为没有从字符串到int的隐式转换,所以您需要显式转换。你可以这样做
int a=Convert.ToInt32Console.ReadLine;或
int a=int.ParseConsole.ReadLine;或
int a=int Console.ReadLine;
还有其他方法。有关详细信息,请访问不要将此类内容放在标题中,它没有意义,没有价值,也不会给你带来任何帮助fasterIt很酷,你正在学习,很好,你共享了代码,但你应该在发布前尝试解决问题,谷歌搜索c转换字符串到int将为你解决这个问题。可能重复