Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to show a question again when a wrong answer is entered?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to show a question again when a wrong answer is entered?

    I wrote the code below for my homework. It should ask the user to calculate two things and it should stop when null is entered. It works, except from one thing and that is that it should show the same question again if you give the wrong answer. What do I have to do to show the same question again when I enter for example 24 to question 1?
    ----------------------------------------------------------
     
    import java.util.Scanner;
    public class SmallMathematicsApplication{
     
     
     
       public static void main(String[] args) {
       int som1;
       int som2;
       int reportError;
     
       Scanner reader = new Scanner(System.in);
     
       System.out.print("5+20? ");
       som1 = reader.nextInt();
     
       if (som1 == 25) {
           System.out.println("good job");}
     
       else if (som1 == 0)   {
           System.out.println("you stopped the program by entering null");System.exit(-1);}
     
       else  {
           System.out.println("try it again");} 
     
       System.out.print("5+26? ");
       som2 = reader.nextInt();
     
       if (som2 == 31) {
           System.out.println("good job, you finished the exercise");}
     
       else if (som2 == 0)   {
           System.out.println("you stopped the program by entering null");System.exit(-1);}
     
       else  {
           System.out.println("try it again");}
     
        }}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to show a question again when a wrong answer is entered?

    Sounds like a job for a do while loop to me.

    Also, your bracket placement is really wonky: I suggest putting your closing brackets on their own line. I almost thought you were asking why your if statements weren't working.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Henk (November 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to show a question again when a wrong answer is entered?

    Works now, thanks a lot. I just started learning Java, have to work on the brackets.

Similar Threads

  1. Replies: 9
    Last Post: August 30th, 2012, 03:25 PM
  2. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM
  3. please answer some basic question
    By togaurav in forum Java Theory & Questions
    Replies: 5
    Last Post: April 16th, 2011, 07:58 AM
  4. Really Quick n00b question, should take three seconds to answer
    By joeschmidt45 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 24th, 2011, 05:23 AM
  5. Replies: 1
    Last Post: December 4th, 2010, 05:26 PM