简体   繁体   English

如何在循环外重用 int。 C#

[英]How do I reuse an int outside of loop. C#

I've created code to read a user's number and change it into an int.我创建了读取用户号码并将其更改为 int 的代码。 Essentially my code looks like this基本上我的代码看起来像这样

while (e != 1)
{
int num = Convert.ToInt32(Console.ReadLine());
e += 1;
}

How do I reuse the 'int num' outside of the loop?如何在循环外重用“int num”?

You can declare num outside of the loop but assign to it inside the loop:您可以在循环外声明num ,但在循环内分配给它:

int num = 0; // Or some other default value
while (e != 1)
{
    num = Convert.ToInt32(Console.ReadLine()); // Note that num is NOT declared here
    e += 1;
}
// Use num here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM