Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Randomly generate integers

  • 05-03-2012 03:37PM
    #1
    Registered Users, Registered Users 2 Posts: 302 ✭✭


    Hi,

    I am developing an app that generates a different equation to solve every time a user presses a button. I am having trouble generating different integers every time the button is pressed. Every time i press the "New" button nothing happens.

    Code:
    Random gen = new Random();

    int num = gen.nextInt(50);
    int num2 = gen.nextInt(50);
    char[] ops = new char[]{'+', '-', '*', '/'};
    int r = gen.nextInt(ops.length);
    char rndOp = ops[r];
    int sum;{

    if (r==0){
    sum = num + num2;

    }
    else if(r==1){
    sum = num - num2;
    }
    else if(r==2){
    sum = num * num2;
    }
    else if(r==3){
    sum = num / num2;
    }
    }

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

    TextView textChange = (TextView) findViewById(R.id.textView);
    textChange.setText(Integer.toString(num) + rndOp + Integer.toString(num2) + "=");

    final Button buttonNew = (Button) findViewById(R.id.keypad_14);
    buttonNew.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
    TextView ans = (TextView) findViewById(R.id.textView);
    ans.setText(Integer.toString(num) + rndOp + Integer.toString(num) + "=");
    }
    });

    Any help would be appreciated!


Comments

  • Registered Users, Registered Users 2 Posts: 1,235 ✭✭✭Odaise Gaelach


    First of all, set a breakpoint in your IDE at the start of the onClick(View v) method, to ensure that it's being called.

    What you think should happen is that every time your code refers to num and num2 it will generate a new random number, which isn't the case. It's generating a random number to initially assign to the integers, but it's not re-assigning a new one every time they're needed.

    So your solution would be to change the onClick method to something like:
    public void onClick(View v)
    {
       TextView ans = (TextView) findViewById(R.id.textView);
       [B]num = gen.nextInt(50);
       num2 = gen.nextInt(50);[/B]
       ans.setText(Integer.toString(num) + rndOp + Integer.toString([B]num2[/B]) + "=");
    }
    

    You're also referring to num twice in the setText line, rather than num and num2. :)


  • Registered Users, Registered Users 2 Posts: 302 ✭✭Corcs001


    Problem solved, thanks!


  • Registered Users, Registered Users 2 Posts: 589 ✭✭✭loctite


    sum = num / num2;

    Is is possible that you are going to get an Arithmetic Exception here too?


  • Closed Accounts Posts: 2,930 ✭✭✭COYW


    loctite wrote: »
    sum = num / num2;

    Is is possible that you are going to get an Arithmetic Exception here too?

    By the sounds of the application, it is beginner level and I suspect that exception handling isn't in play yet.

    OP, if you want to avoid the exception above, add one to the random numbers generated, if the value generated is zero.


Advertisement