Android Question RANDOMIZED CALCULATOR OPERATIONS

bistecc

Member
Hi,
I would like to create a little game with randomized calculator operations (+ - * / ).
While I do not have any problem randomizing numbers, I'm confused about symbols.
Thanks in advance
 

bistecc

Member
Thanks, it works perfectly.? But I thought I could use this variable (operator) in the calculator operation instead of the operator such as:
resultField = num1 operator num2
but it doesn't work in this way. I mean, if I decide the operator in the code it works
resultFiled = num1 + num2 'where resultField is the sum of num1 + num2 and I declared Private resultField As EditText in Sub Globals
what's wrong here?
 
Last edited:
Upvote 0

bistecc

Member
ERRATA CORRIGE
Thanks, it works perfectly.? But I thought I could use this variable (operator) in the calculator operation instead of the operator such as:
resultField = num1 operator num2
but it doesn't work in this way. I mean, if I decide the operator in the code it works
resultFiled = num1 + num2 'where resultField is the sum of num1 + num2 and I declared Private resultField As EditText in Sub Globals
what's wrong here?
 
Upvote 0

bistecc

Member
I think I found a work around. I introduced a if test like:
If operator = "+" Then
resultInt = num1 + num2
Else If operator = "-" Then
resultInt = num1 - num2
.....
and so on for the other operators.
Thank you in advance for best suggestions.
 
Upvote 0

Daica

Active Member
Licensed User
You could use a Select

B4X:
    Dim operators As List = Array("+", "-", "*", "/")
    Dim operator As String = operators.Get(Rnd(0, operators.Size)) 'second Rnd parameter is exclusive
    
    Select operator
        Case "+"
            YourCodeToAdd
        Case "-"
            YourCodeToSubtract
        Case "*"
            YourCodeToMultiply
        Case "/"
            YourCodeToDivide
    End Select
 
Upvote 0

udg

Expert
Licensed User
Longtime User
You may be interested in the Eval class too.
 
Upvote 0
Top