# Thread: Confusion with maths game?...

1. Member
Join Date
Aug 2011
Posts
31
Rep Power
0

## 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 sub;
private int mult;
public int ans;
private int correct;

public Void Method(){

lol = 1 + random.nextInt(3);

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

2. 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.
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

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

kind regards,

Jos
Yeah we got the point ;)

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

kind regards,

Jos

5. Member
Join Date
Aug 2011
Posts
31
Rep Power
0
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. Java Code:
`public Void Method(){`
Remember that Java is case sensitive. Do you really want to return a Void object?

7. 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. Member
Join Date
Aug 2011
Posts
31
Rep Power
0
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. 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

#### Posting Permissions

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