# Confusion with maths game?...

• 08-28-2011, 03:10 PM
Bimz
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. :(yawn):
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!");     } }```

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();     } }```
thank you, Bimz :D:
• 08-28-2011, 03:17 PM
JosAH
Quote:

Originally Posted by Bimz
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. :(yawn):
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
• 08-28-2011, 03:46 PM
DarrylBurke
Quote:

Originally Posted by JosAH
... over and over again ...

kind regards,

Jos

Yeah we got the point ;)
• 08-28-2011, 04:00 PM
JosAH
Quote:

Originally Posted by DarrylBurke
Yeah we got the point ;)

I don't know what happened; I'll delete the duplicate posts ;-)

kind regards,

Jos
• 08-29-2011, 05:34 AM
Bimz
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
• 08-29-2011, 05:49 AM
Junky
Code:

`public Void Method(){`
Remember that Java is case sensitive. Do you really want to return a Void object?
• 08-29-2011, 08:51 AM
DarrylBurke
Quote:

Do you really want to return a Void object?
You can't do that either.
Quote:

The Void class is an uninstantiable placeholder class ...
db
• 08-29-2011, 10:35 AM
Bimz
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?
• 08-29-2011, 12:08 PM
JosAH
Quote:

Originally Posted by Bimz
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