← ゼロから始める C#
レッスン 38 / 39 · 5 XP
ウォームアップ: 数ですか? ウォームアップ
解説と問題文
機械翻訳です。ネイティブスピーカーによる確認はまだです。
行が数でなければ int.Parse はエラーで落ちます。静かな方法は int.TryParse です: true か false を返し、数は out の後の変数に入れます:
string s = Console.ReadLine();
if (int.TryParse(s, out int n))
{
Console.WriteLine($"ok {n}");
}
else
{
Console.WriteLine("not a number");
}
課題. 1 行を読んでください。整数なら ok とその数を空白で区切って出力し、そうでなければ not a number を出力してください。