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 6 of 6

Thread: How to Exita Run ?

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

    Default How to Exita Run ?

    Hi everyone,

    This is my code so far. I was just wondering would anyone know how to 'end the run' after 10 correct answers are input by the user ? I was thinking maybe a For loop but I'm not too sure..

    import java.util.Scanner;
    import java.util.Random;

    public class Doorknob

    {
    public static void main (String args[])
    {
    int x=0, answer=0;
    int number;


    Random generator=new Random();
    Scanner input=new Scanner (System.in);

    do{
    int randomInt = generator.nextInt(12);
    int randomInt2 = generator.nextInt(12);

    System.out.println("");
    System.out.println("What is " + randomInt + " * " + randomInt2 + "?");
    int result = input.nextInt();
    answer = (randomInt*randomInt2);

    if (result == answer)
    {
    System.out.println("Well done");
    }


    else

    while (result != answer)
    {
    System.out.println("Unfortunately, that's not correct. Please try again.");
    System.out.println("");
    System.out.println("What is " + randomInt + " * " + randomInt2 + "?");
    result = input.nextInt();
    if (result == answer)
    {
    System.out.println("Well done");
    }
    }

    x++;
    }
    while(x <= 5);
    }
    }

    Any help would be good,
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to Exita Run ?

    how to 'end the run' after 10 correct answers
    Use a variable to count the correct answers. When the count gets to 10 'end the run'.

    Can you explain what 'end the run' means?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to Exita Run ?

    Basically can I have a word which I enter, using the scanner class, that will end the program ?


    import java.util.Scanner;
     
     
    public class Doorknob
     
    {
    	public static void main (String args[])
    	{
     
     
     
    	int x=0, answer=0;
    	int number;
     
     
     
    	Scanner input=new Scanner (System.in);
     
    	do{
    		ProjectGenerator c = new ProjectGenerator();
     
     
    		System.out.println("");
    		System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
     
    		while(!input.hasNextInt())
    				{
    				System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
    				System.out.println("");
    				System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
    				input.next();
    				}
     
     
     
    		int result = input.nextInt();
    		answer = (c.getRandomInt()*c.getRandomInt2());
     
    			if (result == answer)
    				{
    				System.out.println("Well done");
    				}
     
     
    			else
     
    				while (result != answer)
    				{
    				System.out.println("Unfortunately, that's not correct. Please try again.");
    				System.out.println("");
    				System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
     
    				while(!input.hasNextInt())
    				{
    				System.out.println("Incorrect. Please enter only digits and try again.");//www.stackoverflow.com
    				System.out.println("");
    				System.out.println("What is " + c.getRandomInt() + " * " + c.getRandomInt2() + "?");
    				input.next();
    				}
     
     
     
    				result = input.nextInt();
    					if (result == answer)
    					{
    					System.out.println("Well done");
    					}
     
     
    				}
     
     
     
     
    		x++;
    		}
    	while(x <= 5);
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to Exita Run ?

    end the program
    Calling the System class's exit()method will end the program.
    Compare the user's String input using one of the String class's methods. If there is a match, call the exit() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: How to Exita Run ?

    I'm fairly new to Java. Any tips on how I would go about doing that ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: How to Exita Run ?

    Use an if statement

    how I would go about doing that ?
    can you explain the steps the code should take to "do that"?
    I'm not sure what you mean.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 01:09 PM
  2. [SOLVED] You need to run this first to know what exactly is the problem.
    By MsBottledDream in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2012, 06:20 AM
  3. I Can't Run Anything
    By silverchain05 in forum Android Development
    Replies: 1
    Last Post: July 30th, 2012, 12:59 PM
  4. when i run applet
    By chonch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2011, 03:56 PM
  5. Re: Can't run the method, Please help!!
    By snowman_heman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 31st, 2011, 07:35 PM