0

Scanner Java

Hello, i'm beginner with JAVA wish restart the scanner for enter another numbers...Do you know can i do please ? my code: package claudeDelannoy; import java.util.Scanner; public class ex18racineCarre { public static void main(String[] args) { // programme calcule les racines carrees // stop quand on lui donne 0 // refus valeurs négatives // demander un nombre positif System.out.println("donnez un nombre positif: "); Scanner sc = new Scanner(System.in); double nombre = sc.nextDouble(); sc.close(); do { // refus valeurs négatives if (nombre < 0) { System.out.println("svp positif"); } // calcule les racines carrees if (nombre > 0) { System.out.println("sa racine carree est: " + Math.sqrt(nombre)); break; } } while (nombre != 0);// si on lui donne 0 = stop } }

1st Jun 2020, 10:06 AM
Vincent Lefebvre
Vincent Lefebvre - avatar
2 ответов
+ 1
I usually put my scanner in a loop if I want to repeat the query Scanner userInput = new Scanner (System.in) String restart = ""; do{ //your code System.out.println("insert another number?") ; restart = userInput.nextLine(); } while(restart.equalsIgnoreCase("Y") || restart.equalsIgnoreCase("yes"); As long as user responds with "yes", the code will run again and you'll be able to insert more numbers
1st Jun 2020, 10:22 AM
HNNX 🐿
HNNX 🐿 - avatar
0
Thanks very well, i have done that before with a char 'y'... for this exercice i want the user can enter a number while it s different 0 without asking
1st Jun 2020, 10:34 AM
Vincent Lefebvre
Vincent Lefebvre - avatar