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

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

问题描述

我所寻找的是如何读取,是由命令行(控制台项目)的用户给出的整数。我主要了解C ++,并已开始沿着C#路径。我知道,到Console.ReadLine();只需要一个char /串。因此,在短期我期待本作的整数版本。

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#很多。我没有找到然而一个线程,在其他网站上,该建议从字符转换为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()函数

You can convert the string to integer using Convert.ToInt32() function

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

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

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