
要在C#中将输入读取为整数,请使用Convert.ToInt32()方法。
res = Convert.ToInt32(val);
让我们看看如何 –
Convert.ToInt32 将数字的指定字符串表示形式转换为等效的 32 位有符号整数。
首先,读取控制台输入 –
string val;val = Console.ReadLine();
读取后,将其转换为整数。
int res;res = Convert.ToInt32(val);
让我们看一个示例 –
示例
现场演示
using System;using System.Collections.Generic;class Demo { static void Main() { string val; int res; Console.WriteLine("Input from user: "); val = Console.ReadLine(); // convert to integer res = Convert.ToInt32(val); // display the line Console.WriteLine("Input = {0}", res); }}
输出
Input from user:Input = 0
以下是输出。输入由用户输入。
Input from user: 2Input = 2
以上就是如何在 C# 中将输入读取为整数?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435126.html
微信扫一扫
支付宝扫一扫