![](/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.