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 make a program run multiple times.

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question How to make a program run multiple times.

    I have made a basic math game that asks you questions and tells you if you have answered them correctly or incorrectly. The game runs great! The thing is that it asks you a question one time and after you answer you have to run the program again. I want to get the program to ask ten questions. After that I want to figure out a scoring system for it but the first step is to get it to ask my ten questions. Here is my code.

    package pkgnew;

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

    public class New {

    public static void main(String args[]) {

    //Declare and construct variables
    Random randomnum = new Random();
    int fnum, snum, answer;
    int mathnumber = randomnum.nextInt(20);
    int newnumber = randomnum.nextInt(20);

    //Declare and construct variables of math problem
    fnum = mathnumber;
    snum = newnumber;
    answer = fnum + snum;

    //Output of math problem
    System.out.print(fnum);
    System.out.print("+");
    System.out.println(snum);

    Scanner serena = new Scanner(System.in);
    int userAnswer = serena.nextInt();

    //If user input = answer display "correct"
    if (userAnswer == answer) {
    System.out.println("Correct!");

    //If user input does not = answer display "wrong" and correct answer
    } else {
    System.out.print("Wrong!");
    }

    }
    }


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: How to make a program run multiple times.

    Please wrap your code with [code] tags so that it is easier to read, e.g.,
    [code=java]
    // your code here
    [/code]

    It preserves formatting and provides syntax highlighting, e.g.,
    // your code here

    Study the for statement at The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics). It'll allow you to loop through your code how ever many times you want. After that, you can declare a variable outside the loop, and update the value of the variable with the score inside the loop.

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

    kunimaro15689 (May 18th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program run multiple times.

    Thanks. I fixed my code in a new thread where I was more specific. Using this method I would have to use everything as an integer for the loop to work? I cant make the entire question an integer so I am confused when it comes to how to do it.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to make a program run multiple times.

    Please don't post multiple threads on the same topic. If you decide you need to be more clear, post the clarification in your original thread rather than starting a new one.

    Clarify your latest question. No idea what you're asking.

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to make a program run multiple times.

    When you want your code to run a certain amount of times, you use the For loop. Here's an example,
    for(int i = 0; i < 10; i++)
    {
        //code in here
    }
    You can also ask the user how many times they want to run it.. Example:
    System.out.println("How many times would you like to run this program?");
    int x = scan.nextInt();
    for(int i = 0; i < x; i++)
    {
         //code here
    }
    When you don't know how many times the user wants to run your code you can use a while loop.. Example:
    String answer = "y";
    while(answer.equalsIgnoreCase("y")) //this just means as long as the variable "answer" equals "y" it will continue to loop
    {
        //other code
        System.out.println("\nDo you want to run this application again?(y or n)");
                answer = scan.nextLine();
                answer = answer.substring(0,1);
    }
    Hope this helps!
    Horray for first post!

  7. The Following User Says Thank You to xPhish For This Useful Post:

    kunimaro15689 (May 23rd, 2014)

  8. #6
    Junior Member
    Join Date
    May 2014
    Location
    Java, TX
    Posts
    18
    My Mood
    Where
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program run multiple times.

    Thanks for your help xPhish! It helped a lot!

Similar Threads

  1. Getting program to ask user input three times
    By Java_noob333 in forum Object Oriented Programming
    Replies: 5
    Last Post: February 24th, 2013, 09:03 AM
  2. [SOLVED] Don't understand why it loops through multiple times...
    By Discoveringmypath in forum Loops & Control Statements
    Replies: 4
    Last Post: January 26th, 2013, 09:00 PM
  3. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  4. How to make my Program always run with dimensions 1350 x 650?
    By i3riank in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 29th, 2011, 07:12 AM
  5. Counting How Many Times Program Was Excecuted
    By Override in forum Loops & Control Statements
    Replies: 2
    Last Post: October 30th, 2010, 05:11 PM