0

I am building app for my daughter for math Everything is working great - add, multiply, divide, between, ascending, etc

but in subtract it loads first time, for second time app get's hang - I have to directly exits it, clear it from ram and restart it every time

log cat is not showing any error or evening warning, please guide me - I am beginner (actually first app)

my code

    package youtube.computers.mohammedi.kidsmath;

    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import com.google.android.gms.ads.AdRequest.Builder;
    import com.google.android.gms.ads.AdView;
    import java.util.Random;

    public class Subtract extends GlobalOverride implements OnClickListener {
        int ans;
        Button btn1;
        Button btn2;
        Button btn3;
        Button btn4;
        int max;
        int n1;
        int n2;
        Random f16r;
        int temp;
        TextView txt_add;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_subtract);
        setTitle("Subtraction");
        this.btn1 =  findViewById(R.id.btn1);
        this.btn2 =  findViewById(R.id.btn2);
        this.btn3 =  findViewById(R.id.btn3);
        this.btn4 =  findViewById(R.id.btn4);
        this.txt_add = (TextView) findViewById(R.id.txt_ascen);
        this.btn1.setOnClickListener(this);
        this.btn2.setOnClickListener(this);
        this.btn3.setOnClickListener(this);
        this.btn4.setOnClickListener(this);
        this.max = getSharedPreferences("Math", 0).getInt("add", 10);
        ((AdView) findViewById(R.id.adView)).loadAd(new Builder().build());
        generate();
    }

    public void onClick(View v) {
        this.ans = Integer.parseInt((String) ((Button) v).getText());
        if (this.ans == this.temp) {
            generate();
        } else {
            Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT).show();
        }
    }

    public void generate() {
        this.n1 = GlobalOverride.getRand(1, this.max);
        this.n2 = GlobalOverride.getRand(1, this.max);
        if(n2>n1 && max<=20){
            generate();
        }
        this.temp = this.n1 - this.n2;
        this.txt_add.setText(this.n1 + " - " + this.n2 + " = ?");
        int[] answer = new int[4];
        int count = 0;
        while (count <= 3) {
            int random_integer = GlobalOverride.getRand(this.temp - 2, this.temp + 2);
            if (!exists(random_integer, answer)) {
                answer[count] = random_integer;
                count++;
            }
        }
        if (!exists(this.temp, answer)) {
            answer[GlobalOverride.getRand(0, 3)] = this.temp;
        }
        this.btn1.setText(String.valueOf(answer[0]));
        this.btn2.setText(String.valueOf(answer[1]));
        this.btn3.setText(String.valueOf(answer[2]));
        this.btn4.setText(String.valueOf(answer[3]));
    }

    public boolean exists(int number, int[] array) {
        if (number == -1) {
            return true;
        }
        for (int i : array) {
            if (number == i) {
                return true;
            }
        }
        return false;
        }
    }

GlobalOverride Class.

package youtube.computers.mohammedi.kidsmath;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import java.net.InetAddress;

public class GlobalOverride extends AppCompatActivity {

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_levels) {
            startActivity(new Intent(this, Levels.class));
        }
        if (id == R.id.action_contact) {
            startActivity(new Intent(this, Contact.class));
        }
        return super.onOptionsItemSelected(item);
    }

    protected static int getRand(int lower, int upper) {
        return ((int) (Math.random() * ((double) ((upper + 1) - lower)))) + lower;
    }

    public boolean isInternetAvailable() {
        try {
            if (InetAddress.getByName("google.com").equals("")) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

Layout Output

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user9543272
  • 11
  • 1
  • 5
  • Why are you adding ads in your app that you are building for your daughter ?Also post code related to your GlobalOverride class – Vivek Mishra Aug 21 '18 at 04:05
  • @VivekMishra - `ads` - just for learning new things - GlobalOverride Class added – user9543272 Aug 21 '18 at 04:13
  • https://stackoverflow.com/questions/7961788/math-random-explanation – Vivek Mishra Aug 21 '18 at 04:21
  • @VivekMishra Same issue after replacing random number function (integer) – user9543272 Aug 21 '18 at 04:37
  • Why are you using such complex logic to create simple Math functions? It's not a best approach to create simple with complex, as for me. As for your problem, you should be careful with random numbers. Are you trying to DEBUG your program, in what line it HANGS? – Stanislav Batura Aug 21 '18 at 05:11

1 Answers1

1

I don't understand why you are calling generate() recursively in that first if-statement. My suspicion is, this is not what you intended. You could end up with several layers of recursion. You might want to add a few comments to your code, that would make understanding it much easier.

If you don't want negative numbers, just swap the two integers instead of calling generate():

if(n2>n1 && max<=20){
    int swap = n1;
    n1 = n2;
    n2 = swap;
}
Sven Affeld
  • 311
  • 3
  • 8