
要在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: 2 Input = 2
以上就是如何在 C# 中将输入读取为整数?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号