data, types, objects, and references - Managing your app's data - Let’s help Owen experiment with ability scores, cast, int, double
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.221(Sharpen your pencil)の解答を求めてみる。
コード
Program.cs
using AbilityScore;
AbilityScoreCalculator abilityScore = new AbilityScoreCalculator();
Console.WriteLine(abilityScore.Score);
abilityScore.CalculateAbilityScore();
Console.WriteLine(abilityScore.Score);
コード
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
0
10
%