Android studio: division not working properly for Math game

时间:2018-09-22 23:01:27

标签: java android

Good evening,

I was working on a homework assignment which is creating a math game app, but have ran into an issue with the division portion. I am supposed to generate two random numbers between 10 to 99 and make sure they are divisible. I made it somewhat work but the numbers were not in that range (ie 1440/14), and I then saw that even when the correct answer entered, the game did not mark it as correct. See code (if(divide.isChecked). Thank you!

package com.example.jamanne.fall2018_hw1;

import android.graphics.Color;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.Random;

public class Mainactivity extends AppCompatActivity {

String key;
RadioButton add, subtract, divide, multiply;
Button Problem, Check;
TextView question, bot,Points;
EditText answer;
int count=0;
int score=0;
int temp=100;


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


    add = (RadioButton) findViewById(R.id.add);
    subtract = (RadioButton) findViewById(R.id.subtract);
    divide = (RadioButton) findViewById(R.id.divide);
    multiply = (RadioButton) findViewById(R.id.multiply);
    Problem = (Button) findViewById(R.id.Problem);
    Check = (Button) findViewById(R.id.Check);
    Check.setEnabled(false);

    answer = (EditText) findViewById(R.id.answer);
    question = (TextView) findViewById(R.id.question);
    bot = (TextView) findViewById(R.id.bot);
    Points= (TextView) findViewById(R.id.Points);


    Problem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Check.setEnabled(true);
        if (add.isChecked())
        {
            int add, rand1, rand2;
            Random r = new Random();

            rand1 = r.nextInt(90)+10;
            rand2 = r.nextInt(90)+10;
            add = rand1+rand2;
            question.setText(rand1+"+"+rand2+"= ?");
            key = ""+ add;
            bot.setText(" ");

        }
        if (subtract.isChecked())
        {
            int sub, rand1, rand2;
            Random r = new Random();

            rand1 = r.nextInt(90)+10;
            rand2 = r.nextInt(90)+10;
             sub= rand1-rand2;
            question.setText(rand1+"-"+rand2+"= ?");
            key = " "+ sub;
            bot.setText(" ");
        }
        if (multiply.isChecked())
        {
            int mult, rand1, rand2;
            Random r = new Random();

            rand1 = r.nextInt(90)+10;
            rand2 = r.nextInt(90)+10;
            mult= rand1*rand2;
            question.setText(rand1+"x"+rand2+"= ?");
            key = ""+ mult;
            bot.setText(" ");
        }
        if (divide.isChecked())
        {
            int div, rand1, rand2,k;
            Random r = new Random();

            rand1 = r.nextInt(90)+10;
            rand2 = r.nextInt(90)+10;
            k= rand1*rand2;
            div= rand1/rand2;
            question.setText(k+"/"+rand1+"= ?");
            key = " "+ div;
            bot.setText(" ");
        }
            bot.setText(" ");
        }
    });

    Check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(answer.getText().toString().equals(key))
            {
                bot.setText("Good Job!");
                bot.setTextColor(Color.GREEN);
                score=score+temp;
                Points.setText(""+score+"pts");
                Points.setTextColor(Color.GREEN);
                Check.setEnabled(false);
            }
             else if(count==1) {
                bot.setText("Answer is " + key);
                bot.setTextColor(Color.RED);
                Check.setEnabled(false);
                score=score-temp;
                Points.setText(""+score+"pts");
                Points.setTextColor(Color.RED);
                count--;
            }
            else if (answer.getText().toString().trim().length()!=0){
                bot.setText("One More Chance");
                bot.setTextColor(Color.RED);
                count++;
            }
            if(answer.getText().toString().trim().length()==0){
                bot.setText("Enter an answer");
            }
        }
    });

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    subtract.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    multiply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    divide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


}

}

0 个答案:

没有答案