I need a little help. To get straight to the point, I was trying to create a small set of equations that would allow me to get a set of numbers with a few peculiarities. Basically what I wanted, after pressing a button, was to create four random numbers (from 1 to 9) that, together and subjected 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 is to randomly pick one of the equations and get the set of random numbers i1, i2, i3, i4 in this equation to be 22, this I can not 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 repeatedly until new numbers appear). I even pretended that whenever I hit the button, the right numbers would automatically appear on the first try.
Thanks if you can help me:)