計算機科学のブログ

types and references - Getting the reference - Console, ReadKeyメソッド, KeyChar, 文字列を数値(int, double)に変換、TryParseメソッド

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 4(types and references - Getting the reference)、p.179(Exercise)の解答を求めてみる。

コード

using System;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            AbilityScoreCalculator calculator = new AbilityScoreCalculator();
            while (true)
            {
                calculator.RollResult = ReadInt(calculator.RollResult, "Starting 4d6roll");
                calculator.DivideBy = ReadDouble(calculator.DivideBy, "Divide by");
                calculator.AddAmount = ReadInt(calculator.AddAmount, "Add amount");
                calculator.Minimum = ReadInt(calculator.Minimum, "Minimum");
                calculator.CalculateAbilityScore();
                Console.WriteLine("Calculated ability score: " + calculator.Score);
                Console.WriteLine("Press Q to quit, any other key to continue");
                char keyChar = Console.ReadKey(true).KeyChar;
                if (keyChar == 'Q' || keyChar == 'q')
                {
                    return;
                }
            }
        }
        static int ReadInt(int lastUsedValue, string prompt)
        {
            Console.Write($"{prompt} [{lastUsedValue}]: ");
            string s = Console.ReadLine();
            if (int.TryParse(s, out int result))
            {
                Console.WriteLine($"\tusing value {result}");
                return result;
            }
            Console.WriteLine($"\tusing value {lastUsedValue}");
            return lastUsedValue;
        }
        static double ReadDouble(double lastUsedValue,string prompt)
        {
            Console.Write($"{prompt} [{lastUsedValue}]: ");
            string s = Console.ReadLine();
            if (double.TryParse(s, out double result))
            {
                Console.WriteLine($"\tusing value {result}");
                return result;
            }
            Console.WriteLine($"\tusing value {lastUsedValue}");
            return lastUsedValue;
        }
    }
}

入出力結果(Terminal, Zsh, csi(C# Interactive))

Starting 4d6roll [14]: 
        using value 14
Divide by [1.75]: 
        using value 1.75
Add amount [2]: 
        using value 2
Minimum [3]: 
        using value 3
Calculated ability score: 10
Press Q to quit, any other key to continue
Starting 4d6roll [14]: 
        using value 14
Divide by [1.75]: 2.15
        using value 2.15
Add amount [10]: 5
        using value 5
Minimum [3]: 2
        using value 2
Calculated ability score: 11
Press Q to quit, any other key to continue
Starting 4d6roll [14]: 21
        using value 21
Divide by [2.15]: 
        using value 2.15
Add amount [11]: 
        using value 11
Minimum [2]: 
        using value 2
Calculated ability score: 20
Press Q to quit, any other key to continue