c# - 将用户的多个字符串输入转换为 Int、DateTime 和 Decimal 数据类型
问题描述
好的,所以我是一名在线学生,在使用 C# 的编程课上。我以前从来没有编程过,所以你必须为我把它一直装傻。
这周的任务是:
- 创建一个名为
ConsoleStudentTracking
. - 使用数据类型创建这些变量:
FirstName
:string
数据类型LastName
:string
数据类型StudentId
:int
数据类型BirthDate
:DateTime
数据类型Grade
:decimal
数据类型
- 使用
Console.ReadLine()
方法获取用户输入来填写步骤 2 中的每个变量。 - 使用
Convert
类将用户输入(string
格式)转换为步骤 2 中指定的所需数据类型/格式。 - 以这种格式将这些变量中的所有值写入屏幕:
- 我的名字是姓氏名字
- 我是一名新学生,这是我的第一个项目
- 我的学生证是:无论用户输入什么
- 我的生日是:用户输入的任何日期
- 我的成绩是:用户输入的任何成绩
我观看了我的导师为我们发送的视频,我基本上遵循了相同的步骤,只是替换了变量。我将尝试下面的代码,这是我收到以下错误的 3 条错误消息:
"Line 23: cannot implicitly convert type 'string' to 'int'"
"Line 26: cannot implicitly convert type 'string' to 'System.DateTime'"
"Line 29: cannot implicitly convert type 'string' to 'decimal'"
我不明白什么没有转换,或者为什么。我看的视频做得很好。这是我对代码的尝试:
using System;
namespace ConsoleStudentTracking
{
class Program
{
static void Main(string[] args)
{
string Firstname;
string Lastname;
int Studentid;
DateTime Birthdate;
decimal Grade;
Console.WriteLine("a.-----------------------------");
Console.WriteLine("Enter your first name");
Firstname= Console.ReadLine();
Console.WriteLine("Enter your last name");
Lastname = Console.ReadLine();
Console.WriteLine("b.My name is Lastname + ", " + Firstname");
Console.WriteLine("c. I am a new student, and this is my first program");
Console.WriteLine("d******************************");
Console.WriteLine("Enter your Studentid");
Studentid = Console.ReadLine(); // line 23
Studentid = Convert.ToInt32(Studentid);
Console.WriteLine("Enter your Birthdate");
Birthdate = Console.ReadLine(); // line 26
Birthdate = Convert.ToDateTime(Birthdate);
Console.WriteLine("Enter your grade");
Grade = Console.ReadLine(); // line 29
Grade = Convert.ToDecimal(Grade);
Console.ReadLine();
}
}
}
解决方案
除了@akash 的答案之外,这里还有更多信息,为什么会出现错误:
第 23 行:不能将类型 'string' 隐式转换为 'int'
您Studentid
被定义为int
,这意味着它只能存储int
egers(整数,如 1、2、3、4,...)。但是,通过使用Console.ReadLine()
,您要求用户输入一些文本(即 a string
)。您可以在Microsoft.Docs中查找函数签名:
例如,即使用户键入,1234
这在内部也是string
由字符1
、2
和组成3
的4
。为了将其用作int
eger,您必须对其进行转换。为此,您使用Convert.ToInt32
(在 C#中int
并且Int32
非常相似)。该函数需要 astring
仅由数字组成,并尝试将其转换为int
。
因此,您可以编写以下代码:
// take the input from the user as a string ...
string StudentidAsString = Console.ReadLine(); // line 23
// ... and convert the string consisting of numbers to an integer
Studentid = Convert.ToInt32(StudentIdAsString);
请注意,Convert.ToInt32
当输入格式错误(即它不包含像 1、2、3、4、...这样的数字)时,该函数将失败(即抛出异常)。
“第 26 行:无法将类型 'string' 隐式转换为 'System.DateTime'”
这里的错误与之前的错误类似,只是string
输入不能转换为 a DateTime
,它也是 .NET 中的一种数据类型,只包含日期和时间信息。为此,您可以使用Convert.ToDateTime
. 同样,您可以编写以下代码来更好地说明将字符串格式的日期转换为 a 所需的单独步骤DateTime
:
// take the input from the user as a string ...
string BirthdateAsString = Console.ReadLine(); // line 26
// ... and convert the string to a DateTime
Birthdate = Convert.ToDateTime(BirthdateAsString)
“第 29 行:无法将类型‘字符串’隐式转换为‘十进制’”
同样,错误与之前的错误相似。在这种情况下,字符串 number 需要转换Convert.ToDecimal
为 a decimal
,这是一种也可以保存数字小数的数据类型(与 相比int
)。
// take the input from the user as a string ...
DateTime GradeAsString = Console.ReadLine(); // line 29
// ... and convert the string to a decimal
Grade = Convert.ToDateTime(GradeAsString);
我想这些错误可以通过使用您最喜欢的搜索引擎通过查找C# cannot implicitly convert type 'string' to 'int'"
(产生几个 100'000 次点击)来解决。
希望对你的下一个任务有所帮助。
推荐阅读
- reactjs - 使用 react-redux 根据布尔值在 Grid 中显示一些数据
- python - 动画单个烛台
- android - 为什么会出现此错误?类型错误:对象不是 'instanceof' 的有效参数(评估 'from instanceof Address - REACT-NATIVE
- angular - 提交动态生成的内容作为表单的一部分
- image-processing - CNN中3通道和1通道表示的灰度图像的区别?
- java - Cassandra 关闭导致:java.io.SyncFailedException:同步失败
- javascript - 基于字符串的运算符的弃用警告
- vim - Vim:将地图右移到正常模式(转义)
- reactjs - 如何在 i18next React 中刷新翻译?
- java - 在 chrome 浏览器中进行视觉验证时,为什么 Selenium 没有打开点击的链接?