使用 C# 在一行中读取两个整数
我知道如何让控制台读取两个整数,但每个整数本身就像这样,
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
如果我输入两个数字,即(1 2),值(1 2),无法解析为整数 我想要的是,如果我输入 1 2 那么它会将其视为两个整数
i know how to make a console read two integers but each integer by it self like this
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
if i entered two numbers, i.e (1 2), the value (1 2), cant be parse to integers
what i want is if i entered 1 2 then it will take it as two integers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
一种选择是接受单行输入作为字符串,然后对其进行处理。
例如:
此方法的一个问题是,如果用户未按预期格式输入文本,它将失败(通过抛出
IndexOutOfRangeException
/FormatException
)。如果可能的话,您将必须验证输入。例如,使用正则表达式:
或者:
int.TryParse
尝试将字符串解析为数字。One option would be to accept a single line of input as a string and then process it.
For example:
One issue with this approach is that it will fail (by throwing an
IndexOutOfRangeException
/FormatException
) if the user does not enter the text in the expected format. If this is possible, you will have to validate the input.For example, with regular expressions:
Alternatively:
int.TryParse
to attempt to parse the strings into numbers.您需要类似(没有错误检查代码)的内容,
它读取一行,在空格上拆分并将拆分的字符串解析为整数。当然,实际上您需要检查输入的字符串是否实际上是有效的整数(int.TryParse)。
You need something like (no error-checking code)
This reads a line, splits on whitespace and parses the split strings as integers. Of course in reality you would want to check if the entered strings are in fact valid integers (int.TryParse).
那么你应该首先将它存储在一个字符串中,然后使用空格作为标记来分割它。
Then you should first store it in a string and then split it using the space as token.
将行读入字符串,拆分字符串,然后解析元素。一个简单的版本(需要添加错误检查)是:
Read the line into a string, split the string, and then parse the elements. A simple version (which needs to have error checking added to it) would be:
得益于 LinQ 和正则表达式,在 1 行中(无需类型检查)
in 1 line, thanks to LinQ and regular expression (no type-checking neeeded)
这应该可以根据您的需要工作!
This Should work as per your need!
试试这个:
希望有帮助:)
Try this:
Hope it helps:)
我有一个更简单的解决方案,使用 switch 语句并在每种情况下使用以 ("\n") 开头的 Console.write() 为用户编写一条消息。
下面是在获取用户输入时使用 for 循环填充数组的示例。 * 注意:您不需要编写 for 循环即可使其工作*
使用名为 arrayOfNumbers[] 的整数数组和临时整数变量尝试此示例。在单独的控制台应用程序中运行此代码,并观看如何在同一行上获取用户输入!
这里的魔术是你欺骗了控制台应用程序,秘密是你在编写提示消息的同一行上获取用户输入。 (message=>“输入第一个数字:”)
这使得用户输入看起来像是被插入到同一行中。我承认它有点原始,但它可以满足您的需要,而不必为如此简单的任务浪费时间在复杂的代码上。
I have a much simpler solution, use a switch statement and write a message for the user in each case, using the Console.write() starting with a ("\n").
Here's an example of filling out an array with a for loop while taking user input. * Note: that you don't need to write a for loop for this to work*
Try this example with an integer array called arrayOfNumbers[] and a temp integer variable. Run this code in a separate console application and Watch how you can take user input on the same line!
The magic trick here is that you're fooling the console application, the secret is that you're taking user input on the same line you're writing your prompt message on. (message=>"Enter First Number: ")
This makes user input look like is being inserted on the same line. I admit it's a bit primitive but it does what you need without having to waste your time with complicated code for a such a simple task.