初級 › Attempted to divide by zero の原因と直し方 › パターン3
Attempted to divide by zero の原因と直し方
整数の除算・剰余演算で除数が0のときに発生します。
1 class Program { 2 static void Main(string[] args) { 3 int total = 100; 4 int count = ___; ^ 5 System.Console.WriteLine(total / count); 6 } 7 }
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
at Program.Main(String[] args) in Program.cs:line 5
空欄を埋めてビルドを通す
件数が0のときに合計を割ってしまう典型的なパターンです。平均計算などで起きがちです。
次の問題