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

Thread: I need help making a simple math game in java.?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help making a simple math game in java.?

    Task: Make a game where the computer asks you 10 questions of random numbers either added, subtracted, multiplied, or division/modulus. Have it return the amount of questions you got right. and how long it took.

    Here is my code:
    /**
    A stopwatch accumulates time when it is running. You can 
    repeatedly start and stop the stopwatch. You can use a
    stopwatch to measure the running time of a program.
    */
    public class Stopwatch
    { 
    /**
    Constructs a stopwatch that is in the stopped state
    and has no time accumulated.
    */
    public Stopwatch()
    { 
    reset();
    }
     
    /**
    Starts the stopwatch. Time starts accumulating now.
    */
    public void start()
    { 
    if (isRunning) return;
    isRunning = true;
    startTime = System.currentTimeMillis();
    }
     
    /**
    Stops the stopwatch. Time stops accumulating and is
    is added to the elapsed time.
    */
    public void stop()
    { 
    if (!isRunning) return;
    isRunning = false;
    long endTime = System.currentTimeMillis();
    elapsedTime = elapsedTime + endTime - startTime;
    }
     
    /**
    Returns the total elapsed time.
    @return the total elapsed time
    */
    public long getElapsedTime()
    { 
    if (isRunning) 
    { 
    long endTime = System.currentTimeMillis();
    return elapsedTime + endTime - startTime;
    }
    else
    return elapsedTime;
    }
     
    /**
    Stops the watch and resets the elapsed time to 0.
    */
    public void reset()
    { 
    elapsedTime = 0;
    isRunning = false;
    }
     
    private long elapsedTime;
    private long startTime;
    private boolean isRunning;
    }
    ______________________________________…
    import java.util.Scanner;
    import java.util.Random;
    public class Mathgame {
     
     
     
    public void addition(){
    int count=0;
    Scanner number = new Scanner(System.in);
    for(int question=0; question<10; question++){
    Random generator = new Random(19580427);
    Random generator2 = new Random(19580427);
    int r = generator.nextInt();
    int q = generator2.nextInt();
    System.out.println("Question 1. " + r + " + " + q + "?");
    int answer = number.nextInt();
    if(answer ==(r+q))
    {
    count++;	
    }
    }
     
    }
    public void subtract(){
    int count=0;
    Scanner number = new Scanner(System.in);
    for(int question=0; question<10; question++){
    Random generator = new Random(19580427);
    Random generator2 = new Random(19580427);
    int r = generator.nextInt();
    int q = generator2.nextInt();
    System.out.println("Question 1. " + r + " - " + q + "?");
    int answer = number.nextInt();
    if(answer ==(r-q))
    {
    count++;	
    }
    }
    }
     
     
    public void Multiplication(){
    int count=0;
     
    Scanner number = new Scanner(System.in);
    for(int question=0; question<10; question++){
    Random generator = new Random(19580427);
    Random generator2 = new Random(19580427);
    int r = generator.nextInt();
    int q = generator2.nextInt();
    System.out.println("Question 1. " + r + " X " + q + "?");
    int answer = number.nextInt();
    if(answer ==(r*q))
    {
    count++;	
    }
    }
     
    }
     
    public void Modulus(){
    int count=0;
    Scanner number = new Scanner(System.in);
    for(int question=0; question<10; question++){
    Random generator = new Random(19580427);
    Random generator2 = new Random(19580427);
    int r = generator.nextInt();
    int q = generator2.nextInt();
    System.out.println("Question 1. " + r + " / " + q + "?");
    int answer = number.nextInt();
    if(answer ==(r%q))
    {
    count++;	
    }
    }
    }
     
     
    }
    ______________________________________
    import java.util.Scanner;
    public class MathgameRunner {
     
    public static void main(String[] args) {
    int x=1;
    while(x>0){
    System.out.println("Choose a game to Play!");
    Scanner test=new Scanner(System.in);
    String choice = test.nextLine();
     
     
    if(choice=="addition"){
     
    }
    if(choice=="subtraction"){
     
     
    }
    if(choice=="multiplication"){
     
    }
    if(choice=="modulus"){
     
    }
    else
    {
    System.out.println("Invalid Choice!");
    }
     
     
     
    }
     
    }
     
    }

    This is incomplete because I can not figure out what to put in the Mathgamerunner.java


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I need help making a simple math game in java.?

    Please wrap your code in the code tags, and if you seek help I recommend asking a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help making a simple math game in java.?

    Do you have any problems with the code?
    If so copy and paste here the error messages
    or the program's output with your comments about what is wrong with the output.

    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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: I need help making a simple math game in java.?

    Welcome to javaprogrammingforums.com!

    To allow us to help you, you will want to edit your post and add [code] [/code] tags around your code (check out the FAQ) and you will want to ask an answerable question since I don't see any specific question yet in your post above.

    Best of luck!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help making a simple math game in java.?

    Please edit the code and format the code properly. The statements need to be indented when they are nested. All statements should NOT start in the first column. Nested statements should be indented 3-4 spaces.

    Take the methods in the MathGameRunner class one at a time. Describe what should go in each method and then try to write the code for the method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 5th, 2013, 02:49 PM
  2. Making Game, Need 1-3 People That Know JAVA
    By Stilez in forum Paid Java Projects
    Replies: 2
    Last Post: August 27th, 2012, 02:51 PM
  3. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  4. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM