![](/img/trans.png)
[英]How can I convert a StringContent() type to an object of type <MyModelClass> in C# with Visual Studio 2019
[英]I can't convert to double in C# using visual studio 2019
我需要有关使用 Convert.ToDouble 的帮助。 我正在努力在 C# 中转换为 double,visual studio 给出了一个例外。 我需要安装扩展吗?
这是所有的代码
using System;
namespace VariablesAndDataTypes
{
class Program
{
static void Main(string[] args)
{
//Variables dclarations amd types
string name;
int age;
double salary;
char gender;
bool working;
//Prompt users for Input
Console.Write("Enter your name: ");
name = Console.ReadLine();
Console.Write("Enter your age: ");
age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your salary: ");
salary = Convert.ToDouble(Console.ReadLine()); // the exception is thrown here
Console.Write("Enter your gender (M/F): ");
gender = Convert.ToChar(Console.ReadLine());
Console.Write("Are you working (true/false): ");
working =Convert.ToBoolean(Console.ReadLine());
//Print to Screen
Console.WriteLine("Your name is: " + name);
Console.WriteLine("Your age is: {0}", age);
Console.WriteLine($"Your salary is: {0}" + salary); // when I write a double it gives and error and the console stops responding.
Console.WriteLine("Your gender is: " + gender);
Console.WriteLine("You are Employed: " + working);
}
}
}
请注意:我是编程新手
欢迎使用 C# @Mickey
可能是您错误地使用了字符串插值($)
尝试这个
Console.WriteLine($"Your salary is: {salary}");
如果你分享你得到的例外,那会很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.