Advertisement
kyamaliev

Control-Flow-Intuition

Apr 12th, 2022
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _04._Control_Flow_and_Loops
  4. {
  5.     internal class Program
  6.     {  
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Please enter the first positive integer: ");
  10.             string firstInput = Console.ReadLine();
  11.  
  12.             Console.WriteLine("Please enter the second positive integer: ");
  13.             string secondInput = Console.ReadLine();
  14.  
  15.  
  16.             int firstNumber = int.MinValue;
  17.             bool isFirstInputParsed = int.TryParse(firstInput, out firstNumber);
  18.  
  19.             if (isFirstInputParsed)
  20.             {
  21.                 Console.WriteLine("First input parsed successfully");
  22.                
  23.             }
  24.             else
  25.             {
  26.                 Console.WriteLine("You have entered something but not an integer");
  27.                 Console.WriteLine("You have one more attempt left. Please enter an integer: ");
  28.                 firstInput = Console.ReadLine();
  29.                 int.TryParse(firstInput, out firstNumber);
  30.  
  31.                 if (firstNumber == 0)
  32.                 {  
  33.                     Console.ForegroundColor = ConsoleColor.Red;
  34.                     Console.WriteLine("You have no more attempts left. Goodbye.");
  35.                     //Console.ForegroundColor = ConsoleColor.Gray;
  36.                     Environment.Exit(1);
  37.                 }
  38.             }
  39.  
  40.             int secondNumber;
  41.             bool isSecondInputParsed = int.TryParse(secondInput, out secondNumber);
  42.  
  43.  
  44.  
  45.  
  46.             Console.WriteLine($"The sum of the two numbers is: {firstNumber + secondNumber}");
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement