Know a good article or link that we're missing? Submit it!

Java Beginners

Moderators: zibadian
Number of threads: 942
Number of posts: 2093

This Forum Only
Post New Thread

Report
New to java Posted by Pseudonym134 on 5 Dec 2009 at 1:24 PM
I've never used Java before today so I'm really clueless about the possibilities and such, I've made a really simple math quiz and I'm wondering if there is a better way to make it have more questions other than adding each one manually.

Is there anything that would let me massively condense the amount of code needed for say 30 questions, or any way to let the user input how many questions they want at the start and have that amount created?

I'm guessing that's what Arrays are used for, but I haven't really been able to find any easy to follow articles on using them - I learn alot better by seeing a practical example and changing it through trial and error.

Any help would be appreciated, below is what I have so far.

import java.io.*; 
import java.util.*;

class Test2 {
  public static void main (String[] args) throws IOException {

    BufferedReader Answer = new BufferedReader
      (new InputStreamReader(System.in));

    int num1, num2, correct1, correct2, score;

    int a = (int)(5.0 * Math.random()) + 5;
    int b = (int)(5.0 * Math.random()) + 5;
    int c = (int)(5.0 * Math.random()) + 5;
    int d = (int)(5.0 * Math.random()) + 5;

    System.out.print ("What is " +a+ " * " +b+ "? ");
    System.out.flush(); 
    num1 = Integer.parseInt( Answer.readLine());

if(num1 == a * b) 
      {
        correct1 = 1;
      }
      else{
         correct1 = 0;
      }
      	
    System.out.print ("What is " +c+ " * " +d+ "? ");
    System.out.flush(); 
    num2 = Integer.parseInt( Answer.readLine());

if(num2 == c * d) 
      {
	correct2 = 1;
      }
      else
      {
         correct2 = 0;
      }
  
    score = correct1 + correct2; 
    System.out.println("You scored " + score + " out of 2.");
 } 
Report
Re: New to java Posted by Josh Code on 5 Dec 2009 at 9:24 PM
You don't have to use an array. You could get what you want with a loop.

Here's some cleaner code. Set numQuestions to whatever you want. This little piece of code could ask 1000 questions by just adding 3 more characters to the code.

It includes some helpful comments for users that get the questions wrong too.

import java.util.*;

class Test2 {

private static String[] loserComments = new String[]
{
 "Loosseeerrr!!",
 "you SUCK!!",
 "dummb bum!!",
 "not the brightest.",
 "what an idiot!!!",
 "how could you get that wrong???"
};

  public static void main (String[] args) 
 {

    Scanner console = new Scanner(System.in);

    int num1, score=0;
    int numQuestions = 5;

  for (int i=0;i<numQuestions;i++)
  {

    int a = (int)(10.0 * Math.random());
    int b = (int)(10.0 * Math.random());

    System.out.print ("What is " +a+ " * " +b+ "? ");

    num1 = console.nextInt();

      if(num1 == a * b) 
      {
        score++;
      }
      else
      {
         System.out.println(loserComments[
          (int)(loserComments.length*Math.random())]);
      }

  }
    System.out.println("You scored " + score + " out of "+numQuestions+".");
 } 

}



 
Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2010 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
bootstrapLabs Logo A BootstrapLabs project.