How to terminate a program when the user input the wrong answer three times












-1















I am trying to ask the user to input a number from 0-9. If they input the right number, the program terminates, but if they do not, they have three chances to get the right number until they are asked to re-run the program again.



This is my code so far:



import java.util.Scanner;

public class DoWhileLoop
{
public static void main(String args)
{
int a = 1;
Scanner keyboard = new Scanner(System.in);


do{
a++;
System.out.println("Please enter a number 0-9");
int value = keyboard.nextInt();


if(value == 1)
{
System.out.println("one");
}
else if(value == 2)
{
System.out.println("two");
}
else if(value == 3)
{
System.out.println("three");
}
else if(value == 4)
{
System.out.println("four");
}
else if(value == 5)
{
System.out.println("five");
}
else if(value == 6)
{
System.out.println("six");
}
else if(value == 7)
{
System.out.println("seven");
}
else if(value == 8)
{
System.out.println("eight");
}
else if(value == 9)
{
System.out.println("nine");
}
if (value <= 9) {
break;
}
}while (a <= 3);
System.out.println("Please rerun the program and enter a number 0-9");
}
}


if you have any advice on what to do, it would be greatly appreciated.










share|improve this question

























  • Ideas: 1. Return from main 2. Throw an Exception.

    – markspace
    Nov 24 '18 at 23:28











  • @markspace im fairly new to java.. can you explain what you meant by that?

    – C.Lai
    Nov 24 '18 at 23:32


















-1















I am trying to ask the user to input a number from 0-9. If they input the right number, the program terminates, but if they do not, they have three chances to get the right number until they are asked to re-run the program again.



This is my code so far:



import java.util.Scanner;

public class DoWhileLoop
{
public static void main(String args)
{
int a = 1;
Scanner keyboard = new Scanner(System.in);


do{
a++;
System.out.println("Please enter a number 0-9");
int value = keyboard.nextInt();


if(value == 1)
{
System.out.println("one");
}
else if(value == 2)
{
System.out.println("two");
}
else if(value == 3)
{
System.out.println("three");
}
else if(value == 4)
{
System.out.println("four");
}
else if(value == 5)
{
System.out.println("five");
}
else if(value == 6)
{
System.out.println("six");
}
else if(value == 7)
{
System.out.println("seven");
}
else if(value == 8)
{
System.out.println("eight");
}
else if(value == 9)
{
System.out.println("nine");
}
if (value <= 9) {
break;
}
}while (a <= 3);
System.out.println("Please rerun the program and enter a number 0-9");
}
}


if you have any advice on what to do, it would be greatly appreciated.










share|improve this question

























  • Ideas: 1. Return from main 2. Throw an Exception.

    – markspace
    Nov 24 '18 at 23:28











  • @markspace im fairly new to java.. can you explain what you meant by that?

    – C.Lai
    Nov 24 '18 at 23:32
















-1












-1








-1








I am trying to ask the user to input a number from 0-9. If they input the right number, the program terminates, but if they do not, they have three chances to get the right number until they are asked to re-run the program again.



This is my code so far:



import java.util.Scanner;

public class DoWhileLoop
{
public static void main(String args)
{
int a = 1;
Scanner keyboard = new Scanner(System.in);


do{
a++;
System.out.println("Please enter a number 0-9");
int value = keyboard.nextInt();


if(value == 1)
{
System.out.println("one");
}
else if(value == 2)
{
System.out.println("two");
}
else if(value == 3)
{
System.out.println("three");
}
else if(value == 4)
{
System.out.println("four");
}
else if(value == 5)
{
System.out.println("five");
}
else if(value == 6)
{
System.out.println("six");
}
else if(value == 7)
{
System.out.println("seven");
}
else if(value == 8)
{
System.out.println("eight");
}
else if(value == 9)
{
System.out.println("nine");
}
if (value <= 9) {
break;
}
}while (a <= 3);
System.out.println("Please rerun the program and enter a number 0-9");
}
}


if you have any advice on what to do, it would be greatly appreciated.










share|improve this question
















I am trying to ask the user to input a number from 0-9. If they input the right number, the program terminates, but if they do not, they have three chances to get the right number until they are asked to re-run the program again.



This is my code so far:



import java.util.Scanner;

public class DoWhileLoop
{
public static void main(String args)
{
int a = 1;
Scanner keyboard = new Scanner(System.in);


do{
a++;
System.out.println("Please enter a number 0-9");
int value = keyboard.nextInt();


if(value == 1)
{
System.out.println("one");
}
else if(value == 2)
{
System.out.println("two");
}
else if(value == 3)
{
System.out.println("three");
}
else if(value == 4)
{
System.out.println("four");
}
else if(value == 5)
{
System.out.println("five");
}
else if(value == 6)
{
System.out.println("six");
}
else if(value == 7)
{
System.out.println("seven");
}
else if(value == 8)
{
System.out.println("eight");
}
else if(value == 9)
{
System.out.println("nine");
}
if (value <= 9) {
break;
}
}while (a <= 3);
System.out.println("Please rerun the program and enter a number 0-9");
}
}


if you have any advice on what to do, it would be greatly appreciated.







java command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 23:35









Ivar

2,870113141




2,870113141










asked Nov 24 '18 at 23:26









C.LaiC.Lai

64




64













  • Ideas: 1. Return from main 2. Throw an Exception.

    – markspace
    Nov 24 '18 at 23:28











  • @markspace im fairly new to java.. can you explain what you meant by that?

    – C.Lai
    Nov 24 '18 at 23:32





















  • Ideas: 1. Return from main 2. Throw an Exception.

    – markspace
    Nov 24 '18 at 23:28











  • @markspace im fairly new to java.. can you explain what you meant by that?

    – C.Lai
    Nov 24 '18 at 23:32



















Ideas: 1. Return from main 2. Throw an Exception.

– markspace
Nov 24 '18 at 23:28





Ideas: 1. Return from main 2. Throw an Exception.

– markspace
Nov 24 '18 at 23:28













@markspace im fairly new to java.. can you explain what you meant by that?

– C.Lai
Nov 24 '18 at 23:32







@markspace im fairly new to java.. can you explain what you meant by that?

– C.Lai
Nov 24 '18 at 23:32














4 Answers
4






active

oldest

votes


















0














you can have your code like this instead:



public class DoWhileLoop {
public static void main(String args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int num = rand.nextInt(10);
Boolean correct = false;
int attempts = 0;
do{
if (attempts == 3) {
System.out.println("rerun the program");
break;

}
System.out.println("Enter number 1-9");
int ans = in.nextInt();
if (ans == num) {
correct = true;
System.out.println("Correct! the number is: " + ans);
} else {
attempts++;
}
}while(!correct);
}
}





share|improve this answer
























  • this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

    – C.Lai
    Nov 24 '18 at 23:51













  • @C.Lai check this answer

    – D.B.
    Nov 24 '18 at 23:58











  • having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

    – peter-cs
    Nov 25 '18 at 0:02













  • no no the user just have to input it, it's not a random number

    – C.Lai
    Nov 25 '18 at 0:09



















0














here is a some reading for you : https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



the return statement is used to return a value from a method.



for example if you want to create the class number which is able to divide two number it you can return the resultat of your function and using it like in a System.out.println for example.



public class Number
{

public static void main(String args)
{
System.out.println(divide(4, 5));
System.out.println(divide(4,0));


}

public static float divide(int a, int b)
{
if(b == 0) {
throw new IllegalArgumentException("Error you can't divide by zero !");
}
return a / b;

}

}


For the throw it's a little more complicated, in java it can happens you have exception for example you can't divide by 0. In this case throw an exception in this case an IllegalargumentException.



Finally your method main is well named because this is the main method of your programm, the first one to be executed and if the programm had no error the last one which means if you leave this method you will leave your programm.






share|improve this answer































    0














    You have to move your "a++" into the else statement in the end of the code. Something like this:



    import java.util.Scanner;

    public class DoWhileLoop {
    public static void main(String args) {
    int a = 0;
    Scanner keyboard = new Scanner(System.in);
    do{
    System.out.println("Please enter a number 0-9");
    int value = keyboard.nextInt();

    if(value == 1){System.out.println("one");}
    else if(value == 2) {System.out.println("two");}
    else if(value == 3) {System.out.println("three");}
    else if(value == 4){System.out.println("four");}
    else if(value == 5){System.out.println("five");}
    else if(value == 6){System.out.println("six");}
    else if(value == 7){System.out.println("seven");}
    else if(value == 8){System.out.println("eight");}
    else if(value == 9){System.out.println("nine");}
    if (value <= 9) {break;}
    else {a++;}
    }
    while (a < 3);
    if (a == 3) {System.out.println("You've used all your attemps.");}
    }
    }





    share|improve this answer
























    • ohhh thank you so much

      – C.Lai
      Nov 25 '18 at 0:12



















    0














    Your attempt looks very close to what you want to achieve.



    You currently have the following piece of code which will cause the program to terminate if a user enters any number less than or equal to 9



    if (value <= 9) {
    break;
    }


    In order to strictly meet the requirement that the program terminates if the user enters a number from 0-9 perhaps this check should be something more like:



    if (value >=0 && value <=9) {
    break;
    }


    In java, you can use the && operator within an if statement to ensure that the code inside the if statement only executes if both of the conditions on either side of the && are true.



    Why not also add a print statement which prompts the user that they have entered a correct answer? For example



    if (value >=0 && value <=9) {
    System.out.println("Correct");
    break;
    }


    In terms of your current code, it already exits if a user enters any number greater than 9 more than 3 times, so that is good. I think if you fix the if statement I've mentioned above then you should be closer to the behaviour you expect.



    The last thing to note is that the program will call this line



                System.out.println("Please rerun the program and enter a number 0-9");


    Regardless of how the do-while loop exits. If you want the program to completely terminate when a user enters a correct guess, without printing this line, you can just add a return statement instead of a break:



        if (value >=0 && value <=9) {
    System.out.println("Correct");
    return;
    }


    This will cause the main method to 'return' rather than continuing to execute any more instructions, this will cause the program to terminate






    share|improve this answer


























    • For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

      – Adam McClenaghan
      Nov 25 '18 at 0:12






    • 1





      very well explained, I like it. Thank you for this

      – C.Lai
      Nov 25 '18 at 0:17











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463289%2fhow-to-terminate-a-program-when-the-user-input-the-wrong-answer-three-times%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    you can have your code like this instead:



    public class DoWhileLoop {
    public static void main(String args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();
    int num = rand.nextInt(10);
    Boolean correct = false;
    int attempts = 0;
    do{
    if (attempts == 3) {
    System.out.println("rerun the program");
    break;

    }
    System.out.println("Enter number 1-9");
    int ans = in.nextInt();
    if (ans == num) {
    correct = true;
    System.out.println("Correct! the number is: " + ans);
    } else {
    attempts++;
    }
    }while(!correct);
    }
    }





    share|improve this answer
























    • this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

      – C.Lai
      Nov 24 '18 at 23:51













    • @C.Lai check this answer

      – D.B.
      Nov 24 '18 at 23:58











    • having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

      – peter-cs
      Nov 25 '18 at 0:02













    • no no the user just have to input it, it's not a random number

      – C.Lai
      Nov 25 '18 at 0:09
















    0














    you can have your code like this instead:



    public class DoWhileLoop {
    public static void main(String args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();
    int num = rand.nextInt(10);
    Boolean correct = false;
    int attempts = 0;
    do{
    if (attempts == 3) {
    System.out.println("rerun the program");
    break;

    }
    System.out.println("Enter number 1-9");
    int ans = in.nextInt();
    if (ans == num) {
    correct = true;
    System.out.println("Correct! the number is: " + ans);
    } else {
    attempts++;
    }
    }while(!correct);
    }
    }





    share|improve this answer
























    • this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

      – C.Lai
      Nov 24 '18 at 23:51













    • @C.Lai check this answer

      – D.B.
      Nov 24 '18 at 23:58











    • having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

      – peter-cs
      Nov 25 '18 at 0:02













    • no no the user just have to input it, it's not a random number

      – C.Lai
      Nov 25 '18 at 0:09














    0












    0








    0







    you can have your code like this instead:



    public class DoWhileLoop {
    public static void main(String args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();
    int num = rand.nextInt(10);
    Boolean correct = false;
    int attempts = 0;
    do{
    if (attempts == 3) {
    System.out.println("rerun the program");
    break;

    }
    System.out.println("Enter number 1-9");
    int ans = in.nextInt();
    if (ans == num) {
    correct = true;
    System.out.println("Correct! the number is: " + ans);
    } else {
    attempts++;
    }
    }while(!correct);
    }
    }





    share|improve this answer













    you can have your code like this instead:



    public class DoWhileLoop {
    public static void main(String args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();
    int num = rand.nextInt(10);
    Boolean correct = false;
    int attempts = 0;
    do{
    if (attempts == 3) {
    System.out.println("rerun the program");
    break;

    }
    System.out.println("Enter number 1-9");
    int ans = in.nextInt();
    if (ans == num) {
    correct = true;
    System.out.println("Correct! the number is: " + ans);
    } else {
    attempts++;
    }
    }while(!correct);
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 23:48









    peter-cspeter-cs

    338




    338













    • this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

      – C.Lai
      Nov 24 '18 at 23:51













    • @C.Lai check this answer

      – D.B.
      Nov 24 '18 at 23:58











    • having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

      – peter-cs
      Nov 25 '18 at 0:02













    • no no the user just have to input it, it's not a random number

      – C.Lai
      Nov 25 '18 at 0:09



















    • this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

      – C.Lai
      Nov 24 '18 at 23:51













    • @C.Lai check this answer

      – D.B.
      Nov 24 '18 at 23:58











    • having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

      – peter-cs
      Nov 25 '18 at 0:02













    • no no the user just have to input it, it's not a random number

      – C.Lai
      Nov 25 '18 at 0:09

















    this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

    – C.Lai
    Nov 24 '18 at 23:51







    this is good, but I am trying to have it in my format due to grading rubric, do you have any advice on my code?

    – C.Lai
    Nov 24 '18 at 23:51















    @C.Lai check this answer

    – D.B.
    Nov 24 '18 at 23:58





    @C.Lai check this answer

    – D.B.
    Nov 24 '18 at 23:58













    having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

    – peter-cs
    Nov 25 '18 at 0:02







    having multiple lines of if, and else if is pretty redundant . Especially if you are asking the user to input the correct value of a random number 0-9. How are you getting a "random number" of 0-9 I don't see that in your code. Also your while loop is broken maybe having while(!a==3) .

    – peter-cs
    Nov 25 '18 at 0:02















    no no the user just have to input it, it's not a random number

    – C.Lai
    Nov 25 '18 at 0:09





    no no the user just have to input it, it's not a random number

    – C.Lai
    Nov 25 '18 at 0:09













    0














    here is a some reading for you : https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



    the return statement is used to return a value from a method.



    for example if you want to create the class number which is able to divide two number it you can return the resultat of your function and using it like in a System.out.println for example.



    public class Number
    {

    public static void main(String args)
    {
    System.out.println(divide(4, 5));
    System.out.println(divide(4,0));


    }

    public static float divide(int a, int b)
    {
    if(b == 0) {
    throw new IllegalArgumentException("Error you can't divide by zero !");
    }
    return a / b;

    }

    }


    For the throw it's a little more complicated, in java it can happens you have exception for example you can't divide by 0. In this case throw an exception in this case an IllegalargumentException.



    Finally your method main is well named because this is the main method of your programm, the first one to be executed and if the programm had no error the last one which means if you leave this method you will leave your programm.






    share|improve this answer




























      0














      here is a some reading for you : https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



      the return statement is used to return a value from a method.



      for example if you want to create the class number which is able to divide two number it you can return the resultat of your function and using it like in a System.out.println for example.



      public class Number
      {

      public static void main(String args)
      {
      System.out.println(divide(4, 5));
      System.out.println(divide(4,0));


      }

      public static float divide(int a, int b)
      {
      if(b == 0) {
      throw new IllegalArgumentException("Error you can't divide by zero !");
      }
      return a / b;

      }

      }


      For the throw it's a little more complicated, in java it can happens you have exception for example you can't divide by 0. In this case throw an exception in this case an IllegalargumentException.



      Finally your method main is well named because this is the main method of your programm, the first one to be executed and if the programm had no error the last one which means if you leave this method you will leave your programm.






      share|improve this answer


























        0












        0








        0







        here is a some reading for you : https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



        the return statement is used to return a value from a method.



        for example if you want to create the class number which is able to divide two number it you can return the resultat of your function and using it like in a System.out.println for example.



        public class Number
        {

        public static void main(String args)
        {
        System.out.println(divide(4, 5));
        System.out.println(divide(4,0));


        }

        public static float divide(int a, int b)
        {
        if(b == 0) {
        throw new IllegalArgumentException("Error you can't divide by zero !");
        }
        return a / b;

        }

        }


        For the throw it's a little more complicated, in java it can happens you have exception for example you can't divide by 0. In this case throw an exception in this case an IllegalargumentException.



        Finally your method main is well named because this is the main method of your programm, the first one to be executed and if the programm had no error the last one which means if you leave this method you will leave your programm.






        share|improve this answer













        here is a some reading for you : https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html



        the return statement is used to return a value from a method.



        for example if you want to create the class number which is able to divide two number it you can return the resultat of your function and using it like in a System.out.println for example.



        public class Number
        {

        public static void main(String args)
        {
        System.out.println(divide(4, 5));
        System.out.println(divide(4,0));


        }

        public static float divide(int a, int b)
        {
        if(b == 0) {
        throw new IllegalArgumentException("Error you can't divide by zero !");
        }
        return a / b;

        }

        }


        For the throw it's a little more complicated, in java it can happens you have exception for example you can't divide by 0. In this case throw an exception in this case an IllegalargumentException.



        Finally your method main is well named because this is the main method of your programm, the first one to be executed and if the programm had no error the last one which means if you leave this method you will leave your programm.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 0:02









        Mathieu CochardMathieu Cochard

        134




        134























            0














            You have to move your "a++" into the else statement in the end of the code. Something like this:



            import java.util.Scanner;

            public class DoWhileLoop {
            public static void main(String args) {
            int a = 0;
            Scanner keyboard = new Scanner(System.in);
            do{
            System.out.println("Please enter a number 0-9");
            int value = keyboard.nextInt();

            if(value == 1){System.out.println("one");}
            else if(value == 2) {System.out.println("two");}
            else if(value == 3) {System.out.println("three");}
            else if(value == 4){System.out.println("four");}
            else if(value == 5){System.out.println("five");}
            else if(value == 6){System.out.println("six");}
            else if(value == 7){System.out.println("seven");}
            else if(value == 8){System.out.println("eight");}
            else if(value == 9){System.out.println("nine");}
            if (value <= 9) {break;}
            else {a++;}
            }
            while (a < 3);
            if (a == 3) {System.out.println("You've used all your attemps.");}
            }
            }





            share|improve this answer
























            • ohhh thank you so much

              – C.Lai
              Nov 25 '18 at 0:12
















            0














            You have to move your "a++" into the else statement in the end of the code. Something like this:



            import java.util.Scanner;

            public class DoWhileLoop {
            public static void main(String args) {
            int a = 0;
            Scanner keyboard = new Scanner(System.in);
            do{
            System.out.println("Please enter a number 0-9");
            int value = keyboard.nextInt();

            if(value == 1){System.out.println("one");}
            else if(value == 2) {System.out.println("two");}
            else if(value == 3) {System.out.println("three");}
            else if(value == 4){System.out.println("four");}
            else if(value == 5){System.out.println("five");}
            else if(value == 6){System.out.println("six");}
            else if(value == 7){System.out.println("seven");}
            else if(value == 8){System.out.println("eight");}
            else if(value == 9){System.out.println("nine");}
            if (value <= 9) {break;}
            else {a++;}
            }
            while (a < 3);
            if (a == 3) {System.out.println("You've used all your attemps.");}
            }
            }





            share|improve this answer
























            • ohhh thank you so much

              – C.Lai
              Nov 25 '18 at 0:12














            0












            0








            0







            You have to move your "a++" into the else statement in the end of the code. Something like this:



            import java.util.Scanner;

            public class DoWhileLoop {
            public static void main(String args) {
            int a = 0;
            Scanner keyboard = new Scanner(System.in);
            do{
            System.out.println("Please enter a number 0-9");
            int value = keyboard.nextInt();

            if(value == 1){System.out.println("one");}
            else if(value == 2) {System.out.println("two");}
            else if(value == 3) {System.out.println("three");}
            else if(value == 4){System.out.println("four");}
            else if(value == 5){System.out.println("five");}
            else if(value == 6){System.out.println("six");}
            else if(value == 7){System.out.println("seven");}
            else if(value == 8){System.out.println("eight");}
            else if(value == 9){System.out.println("nine");}
            if (value <= 9) {break;}
            else {a++;}
            }
            while (a < 3);
            if (a == 3) {System.out.println("You've used all your attemps.");}
            }
            }





            share|improve this answer













            You have to move your "a++" into the else statement in the end of the code. Something like this:



            import java.util.Scanner;

            public class DoWhileLoop {
            public static void main(String args) {
            int a = 0;
            Scanner keyboard = new Scanner(System.in);
            do{
            System.out.println("Please enter a number 0-9");
            int value = keyboard.nextInt();

            if(value == 1){System.out.println("one");}
            else if(value == 2) {System.out.println("two");}
            else if(value == 3) {System.out.println("three");}
            else if(value == 4){System.out.println("four");}
            else if(value == 5){System.out.println("five");}
            else if(value == 6){System.out.println("six");}
            else if(value == 7){System.out.println("seven");}
            else if(value == 8){System.out.println("eight");}
            else if(value == 9){System.out.println("nine");}
            if (value <= 9) {break;}
            else {a++;}
            }
            while (a < 3);
            if (a == 3) {System.out.println("You've used all your attemps.");}
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 0:06









            Sergei VoychukSergei Voychuk

            14627




            14627













            • ohhh thank you so much

              – C.Lai
              Nov 25 '18 at 0:12



















            • ohhh thank you so much

              – C.Lai
              Nov 25 '18 at 0:12

















            ohhh thank you so much

            – C.Lai
            Nov 25 '18 at 0:12





            ohhh thank you so much

            – C.Lai
            Nov 25 '18 at 0:12











            0














            Your attempt looks very close to what you want to achieve.



            You currently have the following piece of code which will cause the program to terminate if a user enters any number less than or equal to 9



            if (value <= 9) {
            break;
            }


            In order to strictly meet the requirement that the program terminates if the user enters a number from 0-9 perhaps this check should be something more like:



            if (value >=0 && value <=9) {
            break;
            }


            In java, you can use the && operator within an if statement to ensure that the code inside the if statement only executes if both of the conditions on either side of the && are true.



            Why not also add a print statement which prompts the user that they have entered a correct answer? For example



            if (value >=0 && value <=9) {
            System.out.println("Correct");
            break;
            }


            In terms of your current code, it already exits if a user enters any number greater than 9 more than 3 times, so that is good. I think if you fix the if statement I've mentioned above then you should be closer to the behaviour you expect.



            The last thing to note is that the program will call this line



                        System.out.println("Please rerun the program and enter a number 0-9");


            Regardless of how the do-while loop exits. If you want the program to completely terminate when a user enters a correct guess, without printing this line, you can just add a return statement instead of a break:



                if (value >=0 && value <=9) {
            System.out.println("Correct");
            return;
            }


            This will cause the main method to 'return' rather than continuing to execute any more instructions, this will cause the program to terminate






            share|improve this answer


























            • For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

              – Adam McClenaghan
              Nov 25 '18 at 0:12






            • 1





              very well explained, I like it. Thank you for this

              – C.Lai
              Nov 25 '18 at 0:17
















            0














            Your attempt looks very close to what you want to achieve.



            You currently have the following piece of code which will cause the program to terminate if a user enters any number less than or equal to 9



            if (value <= 9) {
            break;
            }


            In order to strictly meet the requirement that the program terminates if the user enters a number from 0-9 perhaps this check should be something more like:



            if (value >=0 && value <=9) {
            break;
            }


            In java, you can use the && operator within an if statement to ensure that the code inside the if statement only executes if both of the conditions on either side of the && are true.



            Why not also add a print statement which prompts the user that they have entered a correct answer? For example



            if (value >=0 && value <=9) {
            System.out.println("Correct");
            break;
            }


            In terms of your current code, it already exits if a user enters any number greater than 9 more than 3 times, so that is good. I think if you fix the if statement I've mentioned above then you should be closer to the behaviour you expect.



            The last thing to note is that the program will call this line



                        System.out.println("Please rerun the program and enter a number 0-9");


            Regardless of how the do-while loop exits. If you want the program to completely terminate when a user enters a correct guess, without printing this line, you can just add a return statement instead of a break:



                if (value >=0 && value <=9) {
            System.out.println("Correct");
            return;
            }


            This will cause the main method to 'return' rather than continuing to execute any more instructions, this will cause the program to terminate






            share|improve this answer


























            • For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

              – Adam McClenaghan
              Nov 25 '18 at 0:12






            • 1





              very well explained, I like it. Thank you for this

              – C.Lai
              Nov 25 '18 at 0:17














            0












            0








            0







            Your attempt looks very close to what you want to achieve.



            You currently have the following piece of code which will cause the program to terminate if a user enters any number less than or equal to 9



            if (value <= 9) {
            break;
            }


            In order to strictly meet the requirement that the program terminates if the user enters a number from 0-9 perhaps this check should be something more like:



            if (value >=0 && value <=9) {
            break;
            }


            In java, you can use the && operator within an if statement to ensure that the code inside the if statement only executes if both of the conditions on either side of the && are true.



            Why not also add a print statement which prompts the user that they have entered a correct answer? For example



            if (value >=0 && value <=9) {
            System.out.println("Correct");
            break;
            }


            In terms of your current code, it already exits if a user enters any number greater than 9 more than 3 times, so that is good. I think if you fix the if statement I've mentioned above then you should be closer to the behaviour you expect.



            The last thing to note is that the program will call this line



                        System.out.println("Please rerun the program and enter a number 0-9");


            Regardless of how the do-while loop exits. If you want the program to completely terminate when a user enters a correct guess, without printing this line, you can just add a return statement instead of a break:



                if (value >=0 && value <=9) {
            System.out.println("Correct");
            return;
            }


            This will cause the main method to 'return' rather than continuing to execute any more instructions, this will cause the program to terminate






            share|improve this answer















            Your attempt looks very close to what you want to achieve.



            You currently have the following piece of code which will cause the program to terminate if a user enters any number less than or equal to 9



            if (value <= 9) {
            break;
            }


            In order to strictly meet the requirement that the program terminates if the user enters a number from 0-9 perhaps this check should be something more like:



            if (value >=0 && value <=9) {
            break;
            }


            In java, you can use the && operator within an if statement to ensure that the code inside the if statement only executes if both of the conditions on either side of the && are true.



            Why not also add a print statement which prompts the user that they have entered a correct answer? For example



            if (value >=0 && value <=9) {
            System.out.println("Correct");
            break;
            }


            In terms of your current code, it already exits if a user enters any number greater than 9 more than 3 times, so that is good. I think if you fix the if statement I've mentioned above then you should be closer to the behaviour you expect.



            The last thing to note is that the program will call this line



                        System.out.println("Please rerun the program and enter a number 0-9");


            Regardless of how the do-while loop exits. If you want the program to completely terminate when a user enters a correct guess, without printing this line, you can just add a return statement instead of a break:



                if (value >=0 && value <=9) {
            System.out.println("Correct");
            return;
            }


            This will cause the main method to 'return' rather than continuing to execute any more instructions, this will cause the program to terminate







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 0:10

























            answered Nov 25 '18 at 0:00









            Adam McClenaghanAdam McClenaghan

            6615




            6615













            • For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

              – Adam McClenaghan
              Nov 25 '18 at 0:12






            • 1





              very well explained, I like it. Thank you for this

              – C.Lai
              Nov 25 '18 at 0:17



















            • For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

              – Adam McClenaghan
              Nov 25 '18 at 0:12






            • 1





              very well explained, I like it. Thank you for this

              – C.Lai
              Nov 25 '18 at 0:17

















            For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

            – Adam McClenaghan
            Nov 25 '18 at 0:12





            For additional reading on how return statements work in java : docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

            – Adam McClenaghan
            Nov 25 '18 at 0:12




            1




            1





            very well explained, I like it. Thank you for this

            – C.Lai
            Nov 25 '18 at 0:17





            very well explained, I like it. Thank you for this

            – C.Lai
            Nov 25 '18 at 0:17


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463289%2fhow-to-terminate-a-program-when-the-user-input-the-wrong-answer-three-times%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft