繁体   English   中英

Android数学游戏:如何只获得非负减法问题和整数只用于除法?

[英]Android Math game: How to get only non-negative subtraction questions and whole numbers only for division?

目前对于减法问题,您可以得到第一个数字小于第一个数字(即6-10)的问题,并且除法问题可以是十进制答案。

我怎样才能使减法问题总是非负的,整数只能用于除法?

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();
        }
    }
}

布局

<?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>

使用if else语句示例:

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);
    }
}

将此方法添加到程序中

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
}

为减法做这个:

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

这是为了分裂:

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

这将解决您的问题。

在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);

和getRandomFactor()函数:

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;
}

这将确保操作始终产生积极的结果。

首先选择答案 - 正数。 然后选择您减去或除以的数字,另一个正数。 最后 - 根据这两个随机一次计算问题中的第一部分编号。

因此,如果问题是AB = C:您选择C作为随机数C = rand.nextInt(1000),并且B = rand.nextInt(1000),那么A将是B + C.

除法相同:A / B = C->您选择C和B,A = B * C.

我认为这将是一个干净的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM