0

I have a little problem with a function I'm using on an android app. Ihe function is not too hard, it's just a simple calculation with two random integers. The problem is that I have a label that I want to change the text of into the "problem" so that the user can solve it. I know something is wrong with the function but I can't quite figure out what. My closest guess is that the return part is wrong (since it's null), but I couldn't fix it in anyway so far.

This is my code:

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Game extends Activity implements OnClickListener {

private static final String TAG = "Game";

int game[];

public static final String KEY_DIFFICULTY = "com.example.coursework1.dificulty";

public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
    game = getGame(diff);


}

public int[] getGame(int diff) {
    // TODO Auto-generated method stub
    String operators [] =  {"+","-","*","/"};
    Random op1, op2, op3, op4, op5, op6;
    if(diff == getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE)){
        op1 = new Random();
        op2 = new Random();
        int operator = new Random().nextInt(operators.length);
        String random = (operators[operator]);
        int generate1 = op1.nextInt(10);
        int generate2 = op2.nextInt(10);
        int operation;
        if(random == "+"){
            operation = generate1 + generate2;
            TextView setProblemToView = (TextView) findViewById(R.id.problem);
            setProblemToView.setText("generate1" + random + "generate2");
        }
    }


    return null;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
}
}
beatgammit
  • 19,817
  • 19
  • 86
  • 129
jPratas
  • 225
  • 1
  • 6
  • 17

1 Answers1

0

Why using int ruturn while you have to return always null? use it like that:

public void getGame(int diff) {

    String operators [] =  {"+","-","*","/"};
    Random op1, op2, op3, op4, op5, op6;
    if(diff == getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE)){
        op1 = new Random();

        int operator = new Random().nextInt(operators.length);
        String random = (operators[operator]);
        int generate1 = op1.nextInt(10);
        int generate2 = op1.nextInt(10);
        int operation;
        if(  "+".equalsIgnoreCase(random) ){
            operation = generate1 + generate2;
            TextView setProblemToView = (TextView) findViewById(R.id.problem);
            setProblemToView.setText("generate1" + random + "generate2");
        }
    }

}

and call it just like that:

getGame(diff);

Same random problem solved in my edits, check that for more

Community
  • 1
  • 1
Smile2Life
  • 1,921
  • 3
  • 16
  • 30