Java Code Help!

Need help with a math quiz java program

Page 1 of 1

1 Replies - 1400 Views - Last Post: 20 October 2008 - 09:25 AM Rate Topic: -----

#1 shinigami  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Java Code Help!

Posted 20 October 2008 - 08:35 AM

Hello, I am trying to figure out how to make this program work. I am a beginner with Java and I need all the help I can get. First, I am trying to create a Math quiz type program, where the user picks which operation they want to be quizzed on. I would like for the program to display 10 math questions, show how many questions the user got right, and time the user. The code of what I have tried to do is below and I appreciate anyone and everyone for helping me with this. I really want to learn how to use Java. Thank you!


import java.util.*;

public class mathquizloop {
	public static void main(String[] args)
	{
	 final int NUMBER_OF_QUESTIONS = 10; // number of questions
	int correctCount = 0; // Count the number of correct answers
	int count = 0; // Count the number of questions
	String output = "";
 	 Scanner input = new Scanner(System.in);
	long startTime = System.currentTimeMillis();
	
	
		System.out.println("1. Multiplication");
		System.out.println("2. Addition");
		System.out.println("3. Subtraction");
		System.out.println("4. Division");
		int decision;
		decision = input.nextInt();
		if (decision == 1)
		{
			Multiplication();
		}
		if (decision == 2)
		{
			Addition();
		}
		if (decision == 3)
		{
			Subtraction();
		}
		if (decision == 4)
		{
			Division();
		}
		else {
		System.out.println("Those aren't valid numbers.");
			wrongInput ();
		}
	}
	public static void wrongInput(){
	{
		System.out.println("If you are doing Multiplication Type 1.");
				System.out.println("If you are doing Addition, Type 2.");
				System.out.println("If you are doing Subtraction, Type 3");
				System.out.println("If you are doing Division, Type 4");
				Scanner input = new Scanner(System.in);
				int decision;
				decision = input.nextInt();
				if (decision == 1)
				{
					Multiplication();
				}
				if (decision == 2)
				{
					Addition();
				}
				if (decision == 3)
				{
					Subtraction();
				}
				if (decision == 4)
				{
					Division();
				}
				else {
					System.out.println("Those aren't valid numbers.");
					wrongInput();
		}
	}
	public static void Multiplication()
	{
		int mInput1;
		int mInput2;

		Scanner input = new Scanner(System.in);
		System.out.print("What is the first number? ");
		mInput1 = input.nextInt();

		System.out.print("What is the second number? ");
		mInput2 = input.nextInt();

		System.out.println(mInput1 + " times " + mInput2 + " equals " + mInput1 * mInput2);
	}
	public static void Addition()
	{
		int aInput1;
		int aInput2;
		int aAnswer;

		Scanner input = new Scanner(System.in);
		System.out.println("What is the first number?");
		aInput1 = input.nextInt();

		System.out.println("What is the second number?");
		aInput2 = input.nextInt();

		aAnswer = aInput1 + aInput2;
		System.out.println(aInput1 + " plus " + aInput2 + " equals " + aAnswer);
	}
	public static void Subtraction()
	{
		int sInput1;
		int sInput2;
		int sAnswer;

		Scanner input = new Scanner(System.in);
		System.out.println("What is the first number?");
		sInput1 = input.nextInt();

		System.out.println("What is the second number?");
		sInput2 = input.nextInt();

		sAnswer = sInput1 - sInput2;
		System.out.println(sInput1 + " minus " + sInput2 + " equals " + sAnswer);
	}
	public static void Division()
	{
		double dInput1;
		double dInput2;
		double dAnswer;
;
		Scanner input = new Scanner(System.in);
		System.out.println("What is the first number?");
		dInput1 = input.nextDouble();

		System.out.println("What is the second number?");
		dInput2 = input.nextDouble();

	  if (dInput2 == 0){
	  System.out.println("Cannot divide by zero!");
	  }
	  else{
	  dAnswer = (dInput1) /(dInput2);
	  System.out.println(dInput1 + " divided by " + dInput2 + " equals " + dAnswer);
	 
		  // Increase the count
	  count++;

	  output += "\n" + number1 + "+" + number2 + "=" + answer + 
		((number1 + number2 == answer) ? " correct" : " wrong");
	}
		  
	long endTime = System.currentTimeMillis();
	long testTime = endTime - startTime;

	System.out.println("Correct count is " + correctCount + 
	 "\nTest time is " + testTime / 1000 + " seconds\n" + output);
	
	
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Code Help!

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Java Code Help!

Posted 20 October 2008 - 09:25 AM

long startTime = System.currentTimeMillis();
Maybe you wanna move this row down to just after you get the input from the user?
decision = input.nextInt();
-->here<--
if (decision == 1)

otherwise you charge him for the time where he tries to decide which operation to be quized on.

then if you want the user to answer ten questions, you can easily solve his with a for-loop, either inside the methods if you want all questions to be on the same operation or in the main method if you want the user to be able to select questions on different operations.

Other than that, do you experience any errors in your code?

It seems you never query the user for the answer, only for the question which he specifies himself..
You could use a random number generator to generate the two numbers in the question instead.

Then compare the users answer to the correct answer with an if-statement which if it evaluates true, increases some variable that keeps track of the number of correct answers..

This post has been edited by Gloin: 20 October 2008 - 09:19 AM

Was This Post Helpful? 1

Page 1 of 1