为什么我们使用“int.Parse(Console.ReadLine());” ? [英] Why do we use "int.Parse(Console.ReadLine());" ?

查看:169
本文介绍了为什么我们使用“int.Parse(Console.ReadLine());” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,我不理解Parse?整行num1 = int.Parse(Console.ReadLine());是什么?





* C#交换两个数字的程序

* /

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

namespace程序

{

class program

{

static void Main(string [] args)

{

int num1,num2,temp;

Console.Write(& quot; \ n输入第一个数字:& quot; );

num1 = int.Parse(Console.ReadLine());

Console.Write(& quot; \ n输入第二个数字:& quot; );

num2 = int.Parse(Console.ReadLine());

temp = num1;

num1 = num2;

num2 = temp;

Console.Write(& quot; \\\
After Swapping:& quot;);

Console.Write(& \ n第一个数字:& quot; + num1);

Console.Write(& quot; \ nSecond Number:& quot; + num2);

Console.Read();

}

}

}< / pre>

In this program I did not understand by "Parse" ? What does the entire line "num1 = int.Parse(Console.ReadLine());" ?


* C# Program to Swap two Numbers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num1, num2, temp;
Console.Write(&quot;\nEnter the First Number : &quot;);
num1 = int.Parse(Console.ReadLine());
Console.Write(&quot;\nEnter the Second Number : &quot;);
num2 = int.Parse(Console.ReadLine());
temp = num1;
num1 = num2;
num2 = temp;
Console.Write(&quot;\nAfter Swapping : &quot;);
Console.Write(&quot;\nFirst Number : &quot;+num1);
Console.Write(&quot;\nSecond Number : &quot;+num2);
Console.Read();
}
}
}</pre>

推荐答案

真的,你不应该' t。

虽然Raje_在代码中的作用是正确的,但它不允许不可避免的用户错误。当用户犯错时,应用程序崩溃。

更好的方法是使用TryParse:

Really, you shouldn't.
Although Raje_ is right in what the code does, it makes no allowance for the inevitable user error. And when the user makes a mistake, the app crashes.
A better approach is to use TryParse:
int num1;
while (true)
   {
   Console.Write("Enter the First Number : ");
   if (int.TryParse(Console.ReadLine(), out num1))
      {
      break;
      }
   Console.WriteLine("Please enter an integer value!");
   }



我把它放在接受字符串并返回int的方法中。


And I'd put that in a method that accepts a string and returns an int.


< b> Console.ReadLine 从控制台读取输入。当用户按Enter键时,它返回一个字符串。然后我们恢复程序并处理字符串以确定下一步要采取的行动。



来源: http://www.dotnetperls.com/console-readline [ ^ ]





Console.ReadLine reads input from the console. When the user presses enter, it returns a string. We then resume the program and process the string to determine the next action to take.

Source : http://www.dotnetperls.com/console-readline[^]

And
int.Parse(Console.ReadLine());



这意味着您将用户输入值(字符串)转换为整数。



详细信息请查看以上链接。



祝您好运。


It means you are converting user input value(which is a string) in to a integer.

For details go through above link.

Good luck.


您应该查找特定的MSDN文档功能。那么就没有必要在这里问(特别是因为你已经问了一个关于 Convert.ToInt32 的类似问题)。



因此,请使用您首选的搜索引擎输入该术语:

C#int.Parse

您将获得首批点击:

Int32.Parse Method(String)(System) [ ^ ]



For Convert.ToInt32:

Convert.ToInt32 Method(String,Int32)(System) [ ^ ]



提示:

使用搜索结果,最后使用含有神秘部分的内容,而不是包含函数名称的链接。第一个提供更多信息和示例代码。
You should lookup the MSDN documentation for the specific functions. Then there should be no need to ask here (especially because you have already asked a similar question about Convert.ToInt32).

So enter the term using your preferred search engine:
"C# int.Parse"
and you will get among the first hits:
Int32.Parse Method (String) (System)[^]

For Convert.ToInt32:
Convert.ToInt32 Method (String, Int32) (System)[^]

Tip:
With the search results use those with the cryptic part at the end and not the links containing the function name. The first ones provide more information and example code.


这篇关于为什么我们使用“int.Parse(Console.ReadLine());” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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