+ 1

How can i get it when the user inputs the wrong number it restarts the process?

package me.aaron.myfirstproject; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Enter a number between 1 and 10\nMake sure it is actually between 1 and 10"); int num1 = scan.nextInt(); if (num1 > 10){ System.out.println("That is not between 1 and 10!"); } System.out.println("Enter a second number between 10 and 15"); int num2 = scan.nextInt(); if ((num2 < 10) && (num2 > 15)){ System.out.println("That number is not between 10 and 15!"); } int num3 = num1 + num2; System.out.println("The sum of those numbers is " + num3); } }

14th Dec 2018, 12:36 AM
Aaron Soto
Aaron Soto - avatar
3 odpowiedzi
+ 2
You can use while or in this case maybe better do while loop. So, do{ .... Everyrhing you need to do until .... }while(num1 <= 1 || num1 >= 10)); //if this is the range you wanted (1, 10) Same for num2. Don't forget to declare variables num1 and num2 outside the loops. Also, you can't test this on codePlayground. Let us hear about your progress. Cheers. Also #2, condition num2 < 10 && num2 > 15 is always false. So, you need to better form that one, too.
14th Dec 2018, 1:14 AM
vlada
+ 1
Check loops chapter in java course
14th Dec 2018, 1:09 AM
Chriptus13
Chriptus13 - avatar
0
Use a loop. I would recommend the while loop
14th Dec 2018, 2:43 AM
Kevin Astorga