从用户输入读取整数 [英] Reading an integer from user input

查看:23
本文介绍了从用户输入读取整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的是如何读取用户从命令行(控制台项目)给出的整数.我主要了解 C++ 并且已经开始走上 C# 之路.我知道 Console.ReadLine();只需要一个字符/字符串.所以简而言之,我正在寻找这个的整数版本.

What I am looking for is how to read an integer that was given by the user from the command line (console project). I primarily know C++ and have started down the C# path. I know that Console.ReadLine(); only takes a char/string. So in short I am looking for the integer version of this.

只是为了让您了解我在做什么:

Just to give you an idea of what I'm doing exactly:

Console.WriteLine("1. Add account.");
Console.WriteLine("Enter choice: ");
Console.ReadLine(); // Needs to take in int rather than string or char.

为此我找了好久.我在 C 上发现了很多,但在 C# 上没有.然而,我确实在另一个站点上找到了一个线程,建议将 char 转换为 int.我确信必须有一种比转换更直接的方法.

I have been looking for quite a while for this. I have found a lot on C but not C#. I did find however a thread, on another site, that suggested to convert from char to int. I'm sure there has to be a more direct way than converting.

推荐答案

您可以使用 Convert.ToInt32() 函数

int intTemp = Convert.ToInt32(Console.ReadLine());

这篇关于从用户输入读取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆