SHOW:
|
|
- or go back to the newest paste.
| 1 | // SumOfNumbers.cs | |
| 2 | - | // Sum Of (5) integers, entered from standard input |
| 2 | + | |
| 3 | - | // |
| 3 | + | Console.Write("Please enter 5 numbers: ");
|
| 4 | - | // Build with "csc SumOfNumbers.cs" or "mono-csc SumOfNumbers.cs" |
| 4 | + | int sum = 0; |
| 5 | - | // execute with "SumOfNumbers.exe" |
| 5 | + | int numbers; |
| 6 | for (int i = 0; i < 5; i++) | |
| 7 | - | using System; |
| 7 | + | {
|
| 8 | - | class SumOfNumbers |
| 8 | + | do |
| 9 | - | {
|
| 9 | + | {
|
| 10 | - | public static void Main( ) |
| 10 | + | string nString = Console.ReadLine(); |
| 11 | - | {
|
| 11 | + | bool tryParse = int.TryParse(nString, out numbers); |
| 12 | - | const int numbersRequested = 5; // total requested numbers |
| 12 | + | if (tryParse && i < 5) |
| 13 | - | int numbersRead = 0; // current count of correctly read numbers. |
| 13 | + | {
|
| 14 | - | // Should be the same type as numbersRequested |
| 14 | + | Console.Write("Enter the next numbers: ");
|
| 15 | - | // For 5 elements, "byte" type is also good. |
| 15 | + | break; |
| 16 | - | string userInput; // string to hold Console.ReadLine() result. |
| 16 | + | } |
| 17 | - | long result = 0; // Result. Must be big enough to store |
| 17 | + | else |
| 18 | - | // numbersRequested * (max 32bit integers) |
| 18 | + | {
|
| 19 | Console.Write("Invalid number entered .Try again: ");
| |
| 20 | - | Console.Title = "Sum of numbers"; |
| 20 | + | } |
| 21 | - | Console.WriteLine("Sum of {0} integers.", numbersRequested);
|
| 21 | + | } while (i < 5); |
| 22 | - | do |
| 22 | + | sum += numbers; |
| 23 | - | {
|
| 23 | + | } |
| 24 | - | Console.WriteLine("Please enter number: ");
|
| 24 | + | Console.WriteLine("The sum of your numbers is : " + sum); |