How to calculate resistance?
In this case, (battery voltage – LED voltage) / LED current = resistor value. You see, the resistor is being used here as a sponge to sop up the extra power. In any series circuit, the current flowing through all the devices is the same. The voltage across each of the elements is different. So if you know how much current you want flowing through the circuit, and you know how much voltage you want to see across any one element, you can fill in the spaces. In your case, you want 20mA flowing through the circuit, and you want 3V across the LED. That means that 6V must be across the resistor (9-3=6). And, if the voltage across the resistor is 6V and the current through it is 20mA, then its resistance must be 300 ohms (6/0.02). That is the magical tool we call Ohm’s Law.