I am trying to prompt the user to enter an interger between
I am trying to prompt the user to enter an interger between 1 and 6, if the user doesn\'t enter an interger between 1 and 6 then I need to display a message prompting them to try again, after three times entering the wrong interger the program should exit. How do I go about doing that?
Solution
This is the program which counts how many times user enters an invalid number.If he enters invalid number for 3 times at any time(no need consecutively) when he enters invalid number for the 4th time also then the program will exit.
ValidatingNumber.java
import java.util.Scanner;
public class ValidatingNumber {
public static void main(String[] args) {
//Declaring variable
int num,count=0;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/*This loop continue to execute for 4 times
* If the user entered a number which is not in the range
* Then it displays error message.
* and prompts the user to enter again.
* if the user enters 3 times invalid number ,
* if 4th time also he enters invalid number then the program exits.
*/
while(true)
{
//Getting the number entered by the user
System.out.print(\"\ Enter a number (1-6) inclusive :\");
num=sc.nextInt();
//Checking whether the number is less than 1 or greater than 6 or not
if(num<1 || num>6)
{
//Counting how many times user entered invalid number
count++;
//if the user enters invalid number for more than 3 times then the program will exit.
if(count>3)
{
System.out.println(\":: Invalid Number ::\");
System.out.println(\"** Program Exit **\");
//Command to exit the program
System.exit(0);
}
System.out.println(\"Chance#:\"+count);
//Displaying the error message
System.out.println(\":: Invalid Number.Try Again ::\ \");
continue;
}
else
{
//Displaying the number
System.out.println(\"You Entered number :\"+num);
continue;
}
}
}
}
_______________________________
output:
Enter a number (1-6) inclusive :9
Chance#:1
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :0
Chance#:2
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :8
Chance#:3
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :5
You Entered number :5
Enter a number (1-6) inclusive :9
** Program Exit **
________________________________
This is program will validate the user enters a valid number or not.If he enters invalid number for 3 times consecutively.If 4th time if he enters invalid number then program will exity.
If after 3 chance he entered a valid number.Again he will get 3 chances to enter a wong number
ValidatingNumber.java
import java.util.Scanner;
public class ValidatingNumber {
public static void main(String[] args) {
//Declaring variable
int num,count=0;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/*This loop continue to execute for 4 times
* If the user entered a number which is not in the range
* Then it displays error message.
* and prompts the user to enter again.
* if the user enters 3 times invalid number ,
* if 4th time also he enters invalid number then the program exits.
*/
while(true)
{
//Getting the number entered by the user
System.out.print(\"\ Enter a number (1-6) inclusive :\");
num=sc.nextInt();
//Checking whether the number is less than 1 or greater than 6 or not
if(num<1 || num>6)
{
//Counting how many times user entered invalid number
count++;
//if the user enters invalid number for more than 3 times then the program will exit.
if(count>3)
{
System.out.println(\":: Invalid Number ::\");
System.out.println(\"** Program Exit **\");
//Command to exit the program
System.exit(0);
}
System.out.println(\"Chance#:\"+count);
//Displaying the error message
System.out.println(\":: Invalid Number.Try Again ::\ \");
continue;
}
else
{
count=0;
//Displaying the number
System.out.println(\"You Entered number :\"+num);
continue;
}
}
}
}
__________________________________
output1
Enter a number (1-6) inclusive :9
Chance#:1
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :8
Chance#:2
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :7
Chance#:3
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :4
You Entered number :4
Enter a number (1-6) inclusive :9
Chance#:1
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :8
Chance#:2
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :0
Chance#:3
:: Invalid Number.Try Again ::
Enter a number (1-6) inclusive :9
** Program Exit **
__________________________________



