when you select the script and run it entire, thats equivalent to sending each line to the console.
so the second line of your script is being pushed to the console when the first line of your script is asking you to enter a number, that would cause an error
(imagine running line by line but copy and pasting in the 2nd codeline as your response to the first prompt, thats effectively what is happening)
You can however wrap your interactive code in a function
myfunc <- function()
{a=as.numeric(readline(prompt="Enter the value of x: "))
b=as.numeric(readline(prompt="Enter the value of y: "))
print(sum(a,b))}
myfunc()