計算機科学のブログ

data, types, objects, and references - Managing your app's data - XML Documentation Comments(XMLDoc)

Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 4(data, types, objects, and references - Managing your app’s data)、p.218(Sharpen your pencil)の解答を求めてみる。

コード

Program.cs

using AbilityScore;

AbilityScoreCalculator calculator = new AbilityScoreCalculator();
while (true)
{
    calculator.Rollresult = ReadInt(calculator.Rollresult, "Starting 4d6 roll");
    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;
    }
}

/// <summary>
/// Reads string value from the standard input stream, then convert it to int value
/// </summary>
/// <param name="defaultValue">
/// if input value is invalid, return this value
/// </param>
/// <param name="prompt">
/// option message
/// </param>
/// <returns>
/// int value
/// </returns>
static int ReadInt(int defaultValue, string prompt)
{
    Console.Write($"{prompt} [{defaultValue}] ");
    if (int.TryParse(Console.ReadLine(), out int result))
    {
        return result;
    }
    return defaultValue;
}
static double ReadDouble(double defaultValue, string prompt)
{
    Console.Write($"{prompt} [{defaultValue}] ");
    if (double.TryParse(Console.ReadLine(), out double result))
    {
        return result;
    }
    return defaultValue;
}

コード

AbilityScoreCalculator.cs

namespace AbilityScore;

public class AbilityScoreCalculator
{
    public int Rollresult = 14;
    public double DivideBy = 1.75;
    public int AddAmount = 2;
    public int Minimum = 3;
    public int Score;

    public void CalculateAbilityScore()
    {
        double divided = Rollresult / DivideBy;
        int added = AddAmount + (int)divided;
        if (added < Minimum)
        {
            Score = Minimum;
        }
        else
        {
            Score = added;
        }
    }
}

入出力結果(Terminal, Zsh)

% dotnet run
Starting 4d6 roll [14] 10
divide by [1.75] 
Add amount [2] 
Minimum [3] 
Calculated ability score: 7
Press Q to quit, any other key to continue
Starting 4d6 roll [10] 
divide by [1.75] 
Add amount [2] 5
Minimum [3] 
Calculated ability score: 10
Press Q to quit, any other key to continue
Starting 4d6 roll [10] 
divide by [1.75] 
Add amount [5] 
Minimum [3] 
Calculated ability score: 10
Press Q to quit, any other key to continue
Starting 4d6 roll [10] 
divide by [1.75] 
Add amount [5] 10
Minimum [3] 
Calculated ability score: 15
Press Q to quit, any other key to continue
Starting 4d6 roll [10] 
divide by [1.75] 
Add amount [10] 
Minimum [3] 
Calculated ability score: 15
Press Q to quit, any other key to continue
%