1
I need a little help. To get to the point, I was trying to create a small system of equations that would allow me to get a set of numbers with a few small particularities. Basically what I intended, after pressing a button, was to create four random numbers (from 1 to 9) that, together and submitted to certain mathematical equations, the final result was for example 22. Imagine: 8, 2, 5, 1, are four random numbers that allow this equation "(8*2)+5+1=22".
I know how to create the four random numbers and pass them to Textview’s:
Random a = new Random();
int i1 = a.nextInt(10 - 1) + 1;
TextView tv = (TextView) findViewById(R.id.textView1);
Random b = new Random();
int i2 = b.nextInt(10 - 1)+1;
TextView tv2 = (TextView) findViewById(R.id.textView2);
Random c = new Random();
int i3 = c.nextInt(10 - 1)+1;
TextView tv3 = (TextView) findViewById(R.id.textView3);
Random d = new Random();
int i4 = d.nextInt(10 - 1)+1;
TextView tv4 = (TextView) findViewById(R.id.textView4);
Here are the various equations:
int soma = i1 + i2 + i3 + i4;
int mult0 = i1 * i2 * i3 * i4;
int mult1 = (i1*i2*i3)+i4;
int mult2 = (i1*i2)+i3+i4;
int mult3 = i1 * (i2 + i3 + i4);
int mult4 = i2 * (i1+i3+i4);
int mult5 = i3 * (i1+i2+i4);
int mult6 = i4 * (i1+i2+i3);
int mult7 = (i1*i3)+i2+i4;
int mult8 = (i1*i4)+i2+i3;
int mult9 = (i2*i3)+i1+i4;
int mult10 = (i2*i4)+i1+i3;
int mult11 = (i4*i3)+i1+i2;
int mult12 = (i1*i3*i4)+i2;
int mult13 = (i1*i2*i4)+i3;
int mult14 = (i4*i2*i3)+i1;
But the next step, which is to randomly pick one of the equations and get the random number set I1, I2, i3, I4, in that equation to equal 22, that I can’t figure out how to do. I still have this code but it is not doing what I intend,(PS: TF = 22):
if (soma == TF || mult0 == TF || mult1 == TF ||
mult2 == TF || mult3 == TF || mult4 == TF || mult5 == TF ||
mult6 == TF || mult7 == TF || mult8 == TF || mult9 == TF ||
mult10 == TF || mult11 == TF || mult12 == TF || mult13 == TF || mult14 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
This code basically shows the four random numbers that obey one of these equations, but it is not automatically, it is by attempts (you need to press the button several times until new numbers appear). I really intended that every time I pressed the button, the right numbers would automatically appear on the first try.
Thank you if you can help me :)
Thanks Isac!! I changed a few things and I got to what I wanted, it’s in the comments
– Eduardo Brito