Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Randomly generate integers

  • 05-03-2012 2: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