Created
December 14, 2016 22:09
-
-
Save 121jigowatts/35ab7dbaadc48e7cef0010c51f98da77 to your computer and use it in GitHub Desktop.
[C#]流れるHello World!
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; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var width = 30; | |
var queue = QueueInit(width); | |
string target = "Hello World!" + Interval(); | |
try | |
{ | |
while (true) | |
{ | |
foreach (var c in target) | |
{ | |
Console.Write(Shift(queue, c.ToString())); | |
Console.SetCursorPosition(0, Console.CursorTop); | |
System.Threading.Thread.Sleep(100); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(); | |
Console.WriteLine(e.Message); | |
Console.ReadKey(); | |
} | |
} | |
static string Interval() | |
{ | |
var interval = 10; | |
var intervalStr = string.Empty; | |
for (int i = 0; i < interval; i++) | |
{ | |
intervalStr += " "; | |
} | |
return intervalStr; | |
} | |
static Queue<string> QueueInit(int size) | |
{ | |
var strings = new Queue<string>(); | |
var initialStr = " "; | |
for (int i = 0; i < size; i++) | |
{ | |
strings.Enqueue(initialStr); | |
} | |
return strings; | |
} | |
static void FIFO(Queue<string> queue, string s) | |
{ | |
queue.Dequeue(); | |
queue.Enqueue(s); | |
} | |
static string QueueToString(Queue<string> q) | |
{ | |
var array = q.ToArray(); | |
return string.Join("", array); | |
} | |
static string Shift(Queue<string> queue, string s) | |
{ | |
FIFO(queue, s); | |
return QueueToString(queue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment