How to limit input to only numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
27th Apr 2024, 1:17 PM
Timothy
Timothy - avatar
1 Answer
+ 3
You can use a loop to repeatedly prompt the user until they enter a valid number. In the code I use while loop. That will keep running until validInput becomes true. Inside the loop, it prompts the user to enter the temperature in Fahrenheit. It reads the user input using readLine() and attempts to convert it to a Double. If conversion succeeds, the input is considered valid, and the loop exits. Otherwise, it prints an error message and prompts the user again. Take a look: https://sololearn.com/compiler-playground/cdtl1QKvYyTx/?ref=app Here is the tutorial of while loop in swift on Sololearn: https://www.sololearn.com/learn/o-Swift/2341/?ref=app
27th Apr 2024, 1:51 PM
`нттp⁴⁰⁶
`нттp⁴⁰⁶ - avatar