How does energy become quantized in an infinite potential well??
The proper functions (eigenfunctions) of the Hamiltonian are sin(πnz) where z is the dimensionless length z=x/L. The proper values (eigenvalues) are proportional to (πn)2.
Any, I repeat, any wave inside the well can be decomposed in a sum of proper waves with some amplitudes. In general case the wave energy is not certain but dispersed. Only in the eigenstates the energy is certain.