首页 > 解决方案 > 将用户的多个字符串输入转换为 Int、DateTime 和 Decimal 数据类型

问题描述

好的,所以我是一名在线学生,在使用 C# 的编程课上。我以前从来没有编程过,所以你必须为我把它一直装傻。

这周的任务是:

  1. 创建一个名为ConsoleStudentTracking.
  2. 使用数据类型创建这些变量:
    1. FirstName:string数据类型
    2. LastName:string数据类型
    3. StudentId:int数据类型
    4. BirthDate:DateTime数据类型
    5. Grade:decimal数据类型
  3. 使用Console.ReadLine()方法获取用户输入来填写步骤 2 中的每个变量。
  4. 使用Convert类将用户输入(string格式)转换为步骤 2 中指定的所需数据类型/格式。
  5. 以这种格式将这些变量中的所有值写入屏幕:

    1. 我的名字是姓氏名字
    2. 我是一名新学生,这是我的第一个项目

    3. 我的学生证是:无论用户输入什么
    4. 我的生日是:用户输入的任何日期
    5. 我的成绩是:用户输入的任何成绩

我观看了我的导师为我们发送的视频,我基本上遵循了相同的步骤,只是替换了变量。我将尝试下面的代码,这是我收到以下错误的 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();
        }
    }
}

标签: c#variablestype-conversion

解决方案


除了@akash 的答案之外,这里还有更多信息,为什么会出现错误:

第 23 行:不能将类型 'string' 隐式转换为 'int'

Studentid被定义为int,这意味着它只能存储integers(整数,如 1、2、3、4,...)。但是,通过使用Console.ReadLine(),您要求用户输入一些文本(即 a string)。您可以在Microsoft.Docs中查找函数签名:

例如,即使用户键入,1234这在内部也是string由字符12和组成34。为了将其用作integer,您必须对其进行转换。为此,您使用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 次点击)来解决。

希望对你的下一个任务有所帮助。


推荐阅读