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