Results 1 to 9 of 9
  1. #1
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Confusion with maths game?...

    So, ive got a really simple maths game works fine and all. But there is just one thing wrong with it, its supposed to ask 10 question and then output how many you got wright out of 10, but the questions seem to never stop.
    Java Code:
    package apples;
    
    /**
     * @author Bimz
     */
    public class Apples {
        
        public static void main(String[] args) {
            int numz = 0;
            
            System.out.println("Simple maths game! Enjoy!");
            
            tuna tunaObject = new tuna();
            
            while(numz <= 10) {
                tunaObject.Method();
                numz++;
            }
            
            System.out.print("You got " + tunaObject.counter + " out of 10 right!");
        }
    }

    Java Code:
    package apples;
    
    import java.util.Random;
    import java.util.Scanner;
    
    /**
     * @author Bimz
     */
    public class tuna {
        Random random = new Random();
        Scanner scan = new Scanner(System.in);
        public int counter = 0;
        private int rndm1;
        private int rndm2;
        private int lol;
        private int add;
        private int sub;
        private int mult;
        public int ans;
        private int correct;
        
        
        public Void Method(){
            
            lol = 1 + random.nextInt(3);
                
            if(lol == 1)  { //add
                rndm1 = 1 + random.nextInt(50);
                rndm2 = 1 + random.nextInt(50);
                correct = rndm1 + rndm2;
                System.out.println(rndm1 + " + " + rndm2);
                ans = scan.nextInt();
                if(ans == correct) {
                    counter += 1;
                    System.out.println("You got it right!");
                }else {
                    System.out.println("You got it wrong :(");
                }
            }else if(lol == 2) { //sub
                rndm1 = 1 + random.nextInt(100);
                rndm2 = 1 + random.nextInt(100);
                if(rndm1 > rndm2){
                correct = rndm1 - rndm2;
                System.out.println(rndm1 + " - " + rndm2);
                }else {
                correct = rndm2 - rndm1;
                System.out.println(rndm2 + " - " + rndm1);    
                }
                ans = scan.nextInt();
                if(ans == correct) {
                    counter += 1;
                    System.out.println("You got it right!");
                }else {
                    System.out.println("You got it wrong :(");
                }
            }else { //mult
                rndm1 = 1 + random.nextInt(6);
                rndm2 = 1 + random.nextInt(6);
                correct = rndm1 * rndm2;
                System.out.println(rndm1 + " * " + rndm2);
                ans = scan.nextInt();
                if(ans == correct) {
                    counter += 1;
                    System.out.println("You got it right!");
                }else {
                    System.out.println("You got it wrong :(");
                }
            }
            return Method();
        }
    }
    please help
    thank you, Bimz

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bimz View Post
    So, ive got a really simple maths game works fine and all. But there is just one thing wrong with it, its supposed to ask 10 question and then output how many you got wright out of 10, but the questions seem to never stop.
    Java Code:
        public Void Method(){
             ...
            return Method();
        }
    What do you think this does? That method calls itself over and over again and never terminates (it will, when a stack overflow occurs).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    ... over and over again ...

    kind regards,

    Jos
    Yeah we got the point ;)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DarrylBurke View Post
    Yeah we got the point ;)
    I don't know what happened; I'll delete the duplicate posts ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default

    when i remove the "return Method();" theres an error which says that there is no return statement for "Method()" so what would i have to enter instead?

    thank you Bimz

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    public Void Method(){
    Remember that Java is case sensitive. Do you really want to return a Void object?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    Do you really want to return a Void object?
    You can't do that either.
    The Void class is an uninstantiable placeholder class ...
    db

  8. #8
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default

    i was unsure of what to put, seen as ive got a scanner, and a string so i put void. should i return it as a string?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Bimz View Post
    i was unsure of what to put, seen as ive got a scanner, and a string so i put void. should i return it as a string?
    You have to make up your mind: what do you want to return, not what type should your method return. Do you want to signal the caller of that method that the user gave an (in)correct answer? If so, a boolean makes sense. If you want to return the points earned with that question, an int makes sense. It's up to you ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help me with maths on my program!
    By Ruuhkis in forum New To Java
    Replies: 6
    Last Post: 02-17-2011, 06:27 AM
  2. Maths for programming
    By ron2794 in forum New To Java
    Replies: 3
    Last Post: 12-17-2010, 08:17 AM
  3. Maths Packages
    By Anish in forum Advanced Java
    Replies: 7
    Last Post: 11-27-2008, 06:46 AM
  4. Whats wrong with my maths???
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 11-03-2008, 05:52 PM
  5. Replies: 15
    Last Post: 11-30-2007, 05:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •