简体   繁体   中英

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. 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?

You can declare num outside of the loop but assign to it inside the loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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