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

Thread: Hey, I'm new to Java and just have a few queries

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Hey, I'm new to Java and just have a few queries

    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 using notepad and command prompt but 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?

    Instead of having multiple random number variables I'd just have two that were reset each question and instead of putting the same code over I'd just have it once and on a loop until the 30th question, is this possible to do?

    Any help would be appreciated, below is what I have so far - I'd like to emphasis this isn't for school or anything like that, I'm just hoping to learn Java in my free time. Also I don't want somebody to code for me, just point me in the right direction.

    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.");
     }
    Last edited by Pseudonym134; December 5th, 2009 at 03:40 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Hey, I'm new to Java and just have a few queries

    Hello Pseudonym134.

    Welcome to the Java Programming Forums

    I can see what you are doing here and everything you are looking to achieve is possible.

    Please bare with me as I'm working on an example to show you.. I can point you in the right direction but I feel showing you my example will help you to understand better.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Hey, I'm new to Java and just have a few queries

    Hello again Pseudonym134.

    I have written this example for you. It will ask the user how many questions they would like and then ask that amount of questions.

    This is simple code but requires some programming logic. I hope you can see how this all works

    import java.util.*;
     
    public class JpfTest2 {
     
        /**
         * JavaProgrammingForums.com
         */
     
        public static int questions, count, a, b, correct, answer;
     
        public static void askQuestion() {
     
            // Loop by amount of questions
            for(int a = 0; a < questions; a++){
            generateQuestion();
            }
     
            System.out.println("You got " + correct + " questions right.");
     
        }
     
        public static void generateQuestion() {
     
            a = (int) (5.0 * Math.random()) + 5;
            b = (int) (5.0 * Math.random()) + 5;
     
            System.out.println("What is " + a + " * " + b + "? ");
     
            Scanner sc2 = new Scanner(System.in);
            answer = sc2.nextInt();
     
            if(answer == a * b) 
              {
                correct++;
              }
        }
     
     
        public static void main(String[] args) {
     
            System.out.println("Welcome to the Maths Quiz! ");
            System.out.println("How many questions would you like? ");
     
            // Scanner class for user input
            Scanner sc = new Scanner(System.in);
            questions = sc.nextInt();
            System.out.println("I will ask you " + questions + " questions:");
            System.out.println("");
     
            askQuestion();
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. The Following User Says Thank You to JavaPF For This Useful Post:

    Pseudonym134 (December 5th, 2009)

  5. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hey, I'm new to Java and just have a few queries

    Thanks alot, that is really what I was looking for and I'll go over it tommorrow to see which parts are doing what (it's a bit late now for any serious thinking ).