/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/programming-guide/string-to-number/convert/Program.cs
56 строк
2 KB
Genevieve Warren
Fix nullability warnings (remaining /samples/snippets/csharp projects) (#35176)
27 апр 2023, 20:58
Не верифицирован
27 апр 2023, 20:58
ca8ed8a
Код
Авторство
О чём код?
using System; public class ConvertStringExample1 { static void Main(string[] args) { int numVal = -1; bool repeat = true; while (repeat) { Console.Write("Enter a number between −2,147,483,648 and +2,147,483,647 (inclusive): "); string? input = Console.ReadLine(); // ToInt32 can throw FormatException or OverflowException. try { numVal = Convert.ToInt32(input); if (numVal < Int32.MaxValue) { Console.WriteLine("The new value is {0}", ++numVal); } else { Console.WriteLine("numVal cannot be incremented beyond its current value"); } } catch (FormatException) { Console.WriteLine("Input string is not a sequence of digits."); } catch (OverflowException) { Console.WriteLine("The number cannot fit in an Int32."); } Console.Write("Go again? Y/N: "); string? go = Console.ReadLine(); if (go?.ToUpper() != "Y") { repeat = false; } } } } // Sample Output: // Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive): 473 // The new value is 474 // Go again? Y/N: y // Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive): 2147483647 // numVal cannot be incremented beyond its current value // Go again? Y/N: y // Enter a number between -2,147,483,648 and +2,147,483,647 (inclusive): -1000 // The new value is -999 // Go again? Y/N: n