1

Currently for subtraction questions you can get a question where the first number is smaller than the first number (ie 6-10) and questions for division can be decimal answers.

How can I make it so that subtraction questions are always non-negative and whole number answers only for division?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity  {

    int rightBox;
    Button b1;
    Button b2;
    Button b3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button)findViewById(R.id.button1);
        b2 = (Button)findViewById(R.id.button2);
        b3 = (Button)findViewById(R.id.button3);

        createOperation();
    }


    public enum Operation {
        SUM, DIVISION, MULTIPLICATION, SUBTRACTION
    }

    private Operation generateRandOperation() {
        Random rand = new Random();
        int choice = rand.nextInt(4);
        Operation operation = null;

        switch (choice)
        {
            case 0:
                operation = Operation.SUM;
                break;
            case 1:
                operation = Operation.DIVISION;
                break;
            case 2:
                operation = Operation.MULTIPLICATION;
                break;
            case 3:
                operation = Operation.SUBTRACTION;
                break;
        }
        String op = String.valueOf(operation);
        Log.d("Operation: ", op);
        return operation;
    }

    public void createOperation() {

        Operation operation = generateRandOperation();
        Random rand = new Random();
        int num1 = rand.nextInt(1000);
        int num2 = rand.nextInt(1000);
        int wrongAns1 = rand.nextInt(1000);
        int wrongAns2 = rand.nextInt(1000);
        rightBox = rand.nextInt(2);
        String choice = String.valueOf(rightBox);
        Log.d("Right box: ", choice);

        String equationText = String.valueOf(num1) + " " +
                getOperationString(operation) + " " + String.valueOf(num2) + "?";

        TextView displayEq = (TextView) findViewById(R.id.tvDisplay);
        displayEq.setText(equationText);

        float rightAns = calculateAnswer(operation, num1, num2);

        switch(rightBox) {
            case 0:
                b1.setText(String.valueOf(rightAns));
                b2.setText(String.valueOf(wrongAns1));
                b3.setText(String.valueOf(wrongAns2));
                break;
            case 1:
                b1.setText(String.valueOf(wrongAns1));
                b2.setText(String.valueOf(rightAns));
                b3.setText(String.valueOf(wrongAns2));
                break;
            case 2:
                b1.setText(String.valueOf(wrongAns2));
                b2.setText(String.valueOf(wrongAns1));
                b3.setText(String.valueOf(rightAns));
                break;
            default:
                break;
        }

    }

    private float calculateAnswer(Operation operation, int num1, int num2) {

        float ans = 0;

        if (operation == Operation.SUM)
        {
            ans = num1 + num2;
        }
        else if (operation == Operation.SUBTRACTION)
        {
            ans = num1 - num2;
        }
        else if (operation == Operation.DIVISION)
        {
            ans = num1 / num2;
        }
        else if (operation == Operation.MULTIPLICATION)
        {
            ans = num1 * num2;
        }
        String choice = String.valueOf(ans);
        Log.d("ANS", choice);
        TextView answerTv = (TextView) findViewById(R.id.tvPoints);
        answerTv.setText(String.valueOf(ans));
        return ans;
    }

    private String getOperationString(Operation operation) {

        String operationText = "";

        if (operation == Operation.SUM)
        {
            operationText = "+";
        } else if (operation == Operation.MULTIPLICATION)
        {
            operationText = "*";
        } else if (operation == Operation.SUBTRACTION)
        {
            operationText = "-";
        } else if (operation == Operation.DIVISION)
        {
            operationText = "/";
        }
        return operationText;
    }

    public void onClick(View view) {

        boolean correct = false;
        boolean play = true;

        switch(view.getId()) {
            case R.id.button1:
                if (rightBox == 0)
                {
                    correct = true;
                }
                break;
            case R.id.button2:
                if (rightBox == 1)
                {
                    correct = true;
                }
                break;
            case R.id.button3:
                if (rightBox == 2)
                {
                    correct = true;
                }
                break;
        }
        if (correct)
        {
            Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
        }
        if (!correct)
        {
            Toast.makeText(getApplicationContext(), "WRONG!", Toast.LENGTH_SHORT).show();

        }
        if(play)
        {
            createOperation();
        }
    }
}

The layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mathgame.MainActivity">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true"
    android:id="@+id/button1"
    android:onClick="onClick"/>

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button1"
    android:layout_toEndOf="@+id/button1"
    android:layout_marginStart="21dp"
    android:id="@+id/button2"
    android:onClick="onClick"/>

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button2"
    android:layout_toEndOf="@+id/button2"
    android:layout_marginStart="30dp"
    android:id="@+id/button3"
    android:onClick="onClick"/>

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/button2"
    android:layout_marginStart="13dp"
    android:layout_marginTop="78dp"
    android:id="@+id/tvDisplay"/>

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button2"
    android:layout_alignStart="@+id/tvDisplay"
    android:layout_marginTop="66dp"
    android:id="@+id/tvPoints"/>
</RelativeLayout>
noname
  • 305
  • 4
  • 15

4 Answers4

1

Use an if else statement Example:

Random rand = new Random();
    int num1 = rand.nextInt(1000);
    int num2 = rand.nextInt(1000);
if(num1<num2){
    Operation operation = generateRandOperation();
    while(operation == Operation.SUBTRACTION||operation == Operation.DIVISION){
          Operation operation = generateRandOperation();  
    }
}
else{
     Operation operation = generateRandOperation();
}
if(operation == Operation.DIVISION){
    if(isPrime(num1)){
        num1++;
    }
    while(((num1%num2)!=0)&&num1<num2){
        int num2 = rand.nextInt(1000);
    }
}

Add this method to program

public static boolean isPrime(int number){
    for(int i=2; i<number; i++){
       if(number%i == 0){
           return false; //number is divisible so its not prime
       }
    }
    return true; //number is prime now
}
Andrew Nguyen
  • 106
  • 11
1

Do this for subtraction:

else if (operation == Operation.SUBTRACTION)
    {
        ans = Math.abs(num1 - num2);
    }

and this for division:

else if (operation == Operation.DIVISION)
    {
        ans = num1 / num2;
        ans = (float) Integer.valueOf(ans);
    }

This will solve your problem.

AlphaQ
  • 656
  • 8
  • 18
  • I have tried this before. The answer will be positive but the question will still display as 6-10. – noname Nov 22 '16 at 19:12
  • Then use an `if... else` statement. If `num1>num2` and operation is a `Subtraction` operation, then switch `num1` and `num2`. – AlphaQ Nov 22 '16 at 19:15
1

Try this in createRandomOperation() :

int num1 = rand.nextInt(1000);
int num2 = 0;
if(operation == Operation.SUBTRACTION) {
    //Handle edge case when num1 = 0
    while(num1 == 0)
        num1 = rand.nextInt(1000);
    //num2 will always be less than num1 (0 to num - 1)
    if(operation == Operation.SUBTRACTION)
        num2 = rand.nextInt(num1);
}
else if(operation = Operation.DIVISION) {
    //Handle edge case when num1 = 0
    while(num1 == 0)
        num1 = rand.nextInt(1000);
    num2 = getRandomFactor(num1);
}
else
    num2 = rand.nextInt(1000);

And the getRandomFactor() function :

private int getRandomFactor(int num) {
    int i = 1, lastRandomFactor;
    while(i <= num / 2) {
        if(num % i == 0) {
            //Choose whether to return i as a factor or not
            int choice = new Random().nextInt(2);
            if(choice == 1)
                return i;
            lastRandomFactor = i;
        } 
        i++;
    }
    //If no factor was returned so far, return the highest one :
    return lastRandomFactor;
}

This will ensure that the operation will always result in a positive result.

rhari
  • 1,367
  • 1
  • 11
  • 19
0

First choose the answer- a positive number. Then choose the number you subtract or divide by, another positive number. Finally - calculate the first part number in the question based on those two random once.

So if the question is A-B=C : You choose C as a random number C=rand.nextInt(1000), and B=rand.nextInt(1000) ,and so A will be B+C.

Same for division: A/B=C-> You choose C and B, and A=B*C

I think this would be a clean approach.

Hermit
  • 1,214
  • 13
  • 24