Rewrite the logic about the prevention of DivZero

I notice the useless combination of forloop and switch,
so I rewrite it.