Created
January 28, 2017 08:16
-
-
Save jsakamoto/8a84bac19be1e426889ae326e0dd304d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Clear(); | |
Console.WriteLine($"Enter digit numbers ({int.MinValue}~{int.MaxValue})."); | |
Console.WriteLine($"I'll totaling those numbers until enter 0."); | |
var sum = EnumEnteredNumbers() | |
.TakeWhile(n => n != 0) | |
.Sum(); | |
Console.WriteLine($"Totaled number is: {sum}"); | |
} | |
static IEnumerable<int> EnumEnteredNumbers() | |
{ | |
for (;;) | |
{ | |
Console.Write("> "); | |
var line = Console.ReadLine(); | |
var num = default(int); | |
if (int.TryParse(line, out num)) | |
yield return num; | |
else | |
Console.WriteLine($"ERROR: Enter digit number ({int.MinValue}~{int.MaxValue})"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment