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>